簡單說,keep alive 是SQL server在創建每個TCP 鏈接的時候,指定了TCP 協議的keepaliveinterval 和keepalivetime參數。這樣對每一個TCP鏈接,若是該鏈接空閒時間(沒有任何數據交互)超過keepalivetime,TCP協議會自動發出keepalive 包檢測鏈接存活與否。若是keepalive 檢測次數超過註冊表TcpMaxDataRetransmissions定義的值而對方仍是沒有迴應,那麼TCP就認爲該鏈接有問題而關閉它。經過這樣的機制SQL server可以檢測出orphaned connection等問題。web
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。windows
SQL server 2000代碼裏面也有對TCP鏈接指定keep alive屬性,但沒有提供用戶界面給用戶定製修改。SQL server2005使用configuration manager能夠修改keep alive值,可是不能修改keepalive interval。 Keepaliveinterval是hardcoded的1秒。session
Configuration manager的界面以下:app
該值保存在註冊表以下位置:tcp
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.(版本[+實例])\MSSQLServer\SuperSocketNetLib\Tcpide
注意SQL server的Native client也有相似配置,不要和server side 的TCP配置搞混了:函數
Native client的 keep alive 配置保存在以下位置:ui
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Client\SNI(版本號)\tcp\Property(序號)this
原理同樣,但不相互干擾。spa
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
詳見以下文檔:
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; };
有。
參見以下文檔:
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
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報告錯誤。或者鏈接或接收數據出錯。