【转载】SQL server connection KeepAlive
1、什么是SQL server TCP连接的keep Alive?
简单说,keep alive 是SQL server在建立每一个TCP 连接的时候,指定了TCP 协议的keepaliveinterval 和keepalivetime参数。这样对每个TCP连接,如果该连接空闲时间(没有任何数据交互)超过keepalivetime,TCP协议会自动发出keepalive 包检测连接存活与否。如果keepalive 检测次数超过注册表TcpMaxDataRetransmissions定义的值而对方还是没有回应,那么TCP就认为该连接有问题而关闭它。通过这样的机制SQL server能够检测出orphaned connection等问题。
SQL server 对每个TCP连接缺省指定keep alive 为30秒,keepaliveinterval为1秒。Windows TCP配置的TcpMaxDataRetransmissions缺省是5次。就是说,如果TCP连接idle了30秒,那么TCP会发送第一个keepalive检查。如果失败,那么TCP会每隔1秒重发keepalive 包,直到重发5次。如果第五次检测依然失败,则该连接就被close。所以,一个TCP连接如果出现异常问题,大概在35秒的时候就会被close。
2、在那里可以配置SQL server 的keep alive 配置?
SQL server 2000代码里面也有对TCP连接指定keep alive属性,但没有提供用户界面给用户定制修改。SQL server2005使用configuration manager可以修改keep alive值,但是不能修改keepalive interval。 Keepaliveinterval是hardcoded的1秒。
Configuration manager的界面如下:

该值保存在注册表如下位置:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.(版本[+实例])\MSSQLServer\SuperSocketNetLib\Tcp

注意SQL server的Native client也有类似配置,不要和server side 的TCP配置搞混了:

Native client的 keep alive 配置保存在如下位置:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SNI(版本号)\tcp\Property(序号)

3、SQL server的keepalive 和Windows的TCP协议里面的keepalive 是一样的吗?
原理一样,但不相互干扰。
Windows 的TCP协议也有keep alive 配置,位置如下:
HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
OS的TCP协议的keep alive 和SQL server 的keep alive 工作原理一样的,就是在建立TCP连接的时候指定TCP连接的keepalive属性(参见后面描述)。但是SQL server读取自己注册表的keep alive来设置TCP连接属性,不理会windows OS的注册表里面的keepalivetime和keepaliveinterval的值。
如果一个应用程序没有显式调用函数设置TCP连接的keepalive属性,那么他的TCP连接默认使用OS 的TCP配置。OSkeep alive配置默认是关闭的。
有关OS 的TCP配置参考如下文档:
http://support.microsoft.com/kb/314053
KeepAliveInterval
Key: Tcpip\Parameters 
Value Type: REG_DWORD - Time in milliseconds      
Valid Range: 1 - 0xFFFFFFFF      
Default: 1000 (one second)      
Description: This parameter determines the interval that separates keepalive retransmissions until a response is received. After a response is received, KeepAliveTime again controls the delay until the next keepalive transmission. The connection is aborted after the number of retransmissions that are specified by TcpMaxDataRetransmissions are unanswered.
KeepAliveTime
Key: Tcpip\Parameters     
Value Type: REG_DWORD - Time in milliseconds      
Valid Range: 1 - 0xFFFFFFFF      
Default: 7,200,000 (two hours)      
Description: The parameter controls how frequently TCP tries to verify that an idle connection is still intact by sending a keepalive packet. If the remote computer is still reachable and functioning, the remote computer acknowledges the keepalive transmission. By default, keepalive packets are not sent. A program can turn on this feature on a connection
4、SQL server 和OS里面 的TCP的keep alive是如何实现的?
详见如下文档:
http://msdn.microsoft.com/en-us/library/ms741621.aspx
SQL server也是调用如下API,把keepalive参数(lpvInBuffer)pass给这个API:
int WSAIoctl(
__in SOCKET s,
__in DWORD dwIoControlCode,
__in LPVOID lpvInBuffer,
__in DWORD cbInBuffer,
__out LPVOID lpvOutBuffer,
__in DWORD cbOutBuffer,
__out LPDWORD lpcbBytesReturned,
__in LPWSAOVERLAPPED lpOverlapped,
__in LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
该文章里面有如下描述:
SIO_KEEPALIVE_VALS (opcode setting: I, T==3)
Enables or disables the per-connection setting of the TCP keep-alive option which specifies the TCP keep-alive timeout and interval. For more information on the keep-alive option, see section 4.2.3.6 on the Requirements for Internet Hosts—Communication Layers specified in RFC 1122 available at the IETF website. The argument structure for SIO_KEEPALIVE_VALS is specified in the tcp_keepalive structure defined in the Mstcpip.h header file. This structure is defined as follows:
/* Argument structure for SIO_KEEPALIVE_VALS */
struct tcp_keepalive {
    u_long  onoff;
    u_long  keepalivetime;
    u_long  keepaliveinterval;
};
5、Named Pipe也有keepalive设置 吗?
有。
参见如下文档:
http://support.microsoft.com/?id=137983
Named Pipes: Named Pipes are implemented in Server Message Block (SMB) layer on top of other transport protocols such as TCP/IP, NetBEUI, or NWLink IPX/SPX. A thin layer called NetBIOS is typically implemented between the SMB and the transport layer. Therefore, a convenient way of adjusting how long a non-responsive Named Pipes session has to wait before being closed is through adjusting the KeepAlive parameters of the relevant NetBIOS layer. For TCP/IP, the NetBIOS layer involved is NBT (NetBIOS over TCP), and the parameter involved is SessionKeepAlive in the following registry key:
KEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netbt\Parameters
6. 如何看SQL server 的连接?SQL server 会主动关闭连接吗?如何看连接的idle时间?
SQL 2008 R2 查询dm_exec_connections即可:
SELECT
*
FROM [sys].[dm_exec_connections]
比较输出里面的last reads/writes 和现在时间可以大概知道一个连接的idle时间。
SQL server 2000则需要查询sysprocesses表。Last_batch时间代表最近一次执行batch的时间。
SQL server 不会关闭一个正常的TCP连接。除非底层TCP报告错误。或者连接或接收数据出错。
【转载】SQL server connection KeepAlive的更多相关文章
- 转载:有关SQL server connection KeepAlive 的FAQ
		转:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/03/sql-server-connection-keepalive-faq.aspx 1.什么是S ... 
- SQL server connection KeepAlive[转]
		1.什么是SQL server TCP连接的keep Alive? 简单说,keep alive 是SQL server在建立每一个TCP 连接的时候,指定了TCP 协议的keepaliveinter ... 
- WPF数据库连接错误:The user is not associated with a trusted SQL Server connection.
		我当初安装sql server的时候选的Window Authentication mode,没选SQL Server Windows Authentication. 后来做WPF时连接数据库时需要一 ... 
- SQL Server Connection Pooling (ADO.NET)
		SQL Server Connection Pooling (ADO.NET) Connecting to a database server typically consists of severa ... 
- [转】[tip] localhost vs. (local) in SQL Server connection strings
		主要区别在于连接协议不同,前者(localhost)使用TCP协议,后者("(local)")使用NamedPipe协议. Sample code with SQL Server ... 
- 转载:有关SQL server connection Keep Alive 的FAQ(3)
		转载:http://blogs.msdn.com/b/apgcdsd/archive/2012/06/07/sql-server-connection-keep-alive-faq-3.aspx 这个 ... 
- 转载:有关SQL server connection Keep Alive 的FAQ(2)
		转: http://blogs.msdn.com/b/apgcdsd/archive/2012/05/18/sql-server-connection-keep-alive-faq-2.aspx 在下 ... 
- 转载 SQL Server中索引管理之六大铁律
		转载原地址 http://jingyan.baidu.com/article/48a42057c03bd7a924250429.html 索引是以表列为基础的数据库对象.索引中保存着表中排序的索引列, ... 
- 转载——SQL Server数据库性能优化之SQL语句篇
		转载自:http://www.blogjava.net/allen-zhe/archive/2010/07/23/326927.html 1. 按需索取字段,跟“SELECT *”说拜拜 字段的提取一 ... 
随机推荐
- 让菜鸡讲一讲费用流(EK)
			让我再讲一个故事吧. 又有一些小精灵要准备从银月城(S)迁徙到Nibel山(T). 这两个地方之间的道路构成了一个网络. 每个道路都有它自己的容量,这决定了每天有多少小精灵可以同时从这儿通过. 和上一 ... 
- 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(一)
			本文导航 -1. 注册并启用红帽订阅 -2. 使用静态 IP 地址配置网络 -3. 设置服务器的主机名称 -4. 更新或升级最小化安装的 CentOS -5. 安装命令行 Web 浏览器 -6. 安装 ... 
- C# Json 序列化大全--任我行
			public class JsonHelper { /// <summary> /// 将Model转换为Json字符串 /// </summary> /// <type ... 
- 【vim环境配置】在centos6.4上配置vim的一些零碎记录
			上一篇日志已经step by step地实录了如何在本机mac上配置vim开发环境已经各种插件. 有了一定经验之后,开始在实验室远程server上centos6.4的环境下配置vim环境. 这台机器是 ... 
- Python 3基础教程10-全局变量和局部变量
			本文来讲讲全局变量和局部变量,前面学习了函数的基本使用,所以,这里就要注意变量的使用和访问权限. 试试下面的demo.py 
- postman与charles的结合使用
			1.准备charles环境 Charles端口一般配置的为8888,不知道怎么配置详见charles文档 打开charles,发现访问浏览器任意页面都是失败. 在浏览器的高级设置中设置代理服务器,以火 ... 
- NGUI注册事件的三种方式
			1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ... 
- 孤荷凌寒自学python第九天Python的输出print的格式化
			孤荷凌寒自学python第九天Python的输出print的格式化 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) (今天感觉手写笔记整得清楚些,汇总电子 笔记时,自己思路凌乱了,练习过程也还 ... 
- Python-map、filter、reduce方法
			介绍 1.map()函数,会让列表中每一个元素都执行一某个函数(传递1个参数), 并且将执行函数返回的结果(无论是什么结果)放在结果列表中 2.filter()函数,会让列表中的每一个元素都执行一次某 ... 
- 团队项目-第七次scrum 会议
			时间:11.3 时长:30分钟 地点:F楼1039教室 工作情况 团队成员 已完成任务 待完成任务 解小锐 学习cocos creator样例 修复员工招聘时bug 陈鑫 完成fire()与UI的对接 ... 
