转载:有关SQL server connection KeepAlive 的FAQ
转:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/03/sql-server-connection-keepalive-faq.aspx
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\SNI9.0\tcp
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 2005查询dm_exec_connections即可:
select session_id from sys.dm_exec_connections
Screen clipping taken: 20/08/2013 15:09
比较输出里面的last reads/writes 和现在时间可以大概知道一个连接的idle时间。
SQL server 2000则需要查询sysprocesses表。Last_batch时间代表最近一次执行batch的时间。
SQL server 不会关闭一个正常的TCP连接。除非底层TCP报告错误。或者连接或接收数据出错。
7、相关参考文档
SQL server TCP的keep alive
http://blogs.msdn.com/sql_protocols/archive/2006/03/09/546852.aspx
DBNetLib sets hard-coded KeepAliveTime and KeepAliveInterval
http://support.microsoft.com/kb/816649
TCP Keep-Alive Messages
http://msdn.microsoft.com/en-us/library/ms819735.aspx
转载:有关SQL server connection KeepAlive 的FAQ的更多相关文章
- 【转载】SQL server connection KeepAlive
1.什么是SQL server TCP连接的keep Alive? 简单说,keep alive 是SQL server在建立每一个TCP 连接的时候,指定了TCP 协议的keepaliveinter ...
- 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 在下 ...
- C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用
C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备) https://blog.csdn.net/u013519551/article/details/51220841 1. . ...
- 【转载】Sql Server参数化查询之where in和like实现详解
文章导读 拼SQL实现where in查询 使用CHARINDEX或like实现where in 参数化 使用exec动态执行SQl实现where in 参数化 为每一个参数生成一个参数实现where ...
随机推荐
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q54-Q56)
Question 54You create custom code to import content to SharePoint sites.You create a custom site def ...
- Ubuntu 14.04下安装JDK8
本文地址:http://www.cnblogs.com/archimedes/p/ubuntu-jdk8.html,转载请注明源地址. 欢迎关注我的个人博客:www.wuyudong.com, 更多云 ...
- Hibernate框架的基本搭建(一个小的java project的测试向数据库中插入和查询数据的功能)
Hibernate介绍:Hibernate是一种“对象-关系型数据映射组件”,它使用映射文件将对象(object)与关系型数据(Relational)相关联,在Hibernate中映射文件通常以&qu ...
- iOS 简单工厂模式
iOS 简单工厂模式 什么是简单工厂模式? 简单工厂模式中定义一个抽象类,抽象类中声明公共的特征及属性,抽象子类继承自抽象类,去实现具体的操作.工厂类根据外界需求,在工厂类中创建对应的抽象子类实例并传 ...
- iOS 内存管理(一)之基础知识介绍
1,什么是引用计数 所有OC对象都有一个计数器,叫做引用计数,引用计数就是目前有几个对象在使用该对象(持有该对象的引用): 2,什么是对象所有权 A对象拥有B对象的引用,A对象拥有B对象的所有权: 3 ...
- 我的android学习经历11
让TextViews实现跑马灯效果 有时候用文本控件时,文本只能在一行显示,而且文本很长的话,后面的文本就会隐藏 一.假如你只需要一个TextView,那个可以添加三个属性实现跑马灯效果,也就是让文字 ...
- 读书笔记-Autonomous Intelligent Vehicles(一)
Autonomous intelligent vehicles have to finish the basic procedures: perceiving and modeling environ ...
- IOS之UI--小实例项目--添加商品和商品名(纯代码终结版)
前言:这个小实例项目是完完全全以MJ视频传授的优化方案一步一个思路从零开始敲出代码的,而且每一步都有思路,都有逻辑所以然.敲代码讲究思路,我个人不建议记忆太多东西,反正我记性很差的. 小贴士:文章末尾 ...
- 谷歌浏览器Chrome安装,报0x80004002 等错误时,乖乖的这么解决!亲测有效。
1.卸载现有的谷歌浏览器,推荐使用360卸载(最好清扫残留文件) 2.在百度搜索中下载一个离线的 chrome浏览器(准备在安全模式先安装),记住,一定不能是谷歌官网的离线包(我屡次尝试安装官方离线包 ...
- 我眼中的自己ps:自我介绍
我是一个理科生,学的是计算机科学与技术专业,目前在桂林理工大学信息科学与工程学院就读.我心里不是很喜欢这个专业,但是因为当初的某种原因就入了这个门.自从学习了计算机以后,我发现这个专业可谓“博大精深, ...