SQL Server 2012中,Sleeping Session表示当前没有运行任何Request,
MSDN 文档的定义:Sleeping - Currently running no requests
如果一个Session 没有运行任何Request,那么该 Session 为什么不结束,而要保持Sleeping Status?
一,Sleeping Session虽然没有运行任何Request,但是,和SQL Server的Connection并没有断开,可能原因主要有两个:
- Session中存在 Transaction 没有commit;
- Session的中所有Transaction都已经提交,仅仅是没有运行任何Request。
A session with that status of sleeping is simply a client connection with no active query to the SQL Server. If the client has an open transaction and the client did not submit a commit or rollback command the state is sleeping .
二,查看Sleeping Session 开启的 transaction和最后执行的SQL 语句
1,查看Sleeping Session 开启的Transaction
SELECT db_name(dt.database_id) as database_name, dt.transaction_id, st.session_id, dt.database_transaction_begin_time, CASE dt.database_transaction_type WHEN 1 THEN ‘Read/write transaction‘ WHEN 2 THEN ‘Read-only transaction‘ WHEN 3 THEN ‘System transaction‘ END database_transaction_type, CASE dt.database_transaction_state WHEN 1 THEN ‘The transaction has not been initialized.‘ WHEN 3 THEN ‘The transaction has been initialized but has not generated any log recorst.‘ WHEN 4 THEN ‘The transaction has generated log recorst.‘ WHEN 5 THEN ‘The transaction has been prepared.‘ WHEN 10 THEN ‘The transaction has been committed.‘ WHEN 11 THEN ‘The transaction has been rolled back.‘ WHEN 12 THEN ‘The transaction is being committed. In this state the log record is being generated, but it has not been materialized or persisted‘ END database_transaction_state, dt.database_transaction_log_record_count, dt.database_transaction_log_bytes_used, dt.database_transaction_log_bytes_reserved FROM sys.dm_tran_database_transactions dt INNER JOIN sys.dm_tran_session_transactions st ON st.transaction_id = dt.transaction_id inner join sys.dm_exec_sessions s on st.session_id=s.session_id where s.status=‘sleeping‘
2,使用DBCC InputBuffer查看Sleeping Session最后执行的SQL 语句
dbcc inputbuffer(sleeping_session_id)
三,Sleeping Session可能会产生阻塞
虽然Sleeping Session占用的资源特别少,但是,如果Sleeping Session打开的Transaction不能及时关闭,在某些特定情况下,不仅会阻止Transaction Log的截断(backup log 能够截断Transaction log,减少日志文件的增长,避免 Disk 空间耗尽),甚至会阻塞其他查询。因此,在产品环境中,应当避免出现Sleeping Session。在Design上,确保在打开一个Connection,执行完相应的查询语句之后,及时提交Transaction,关闭Connection。
引用《Active Request, Sleeping Session》:
Sleeping means that the connection is established and not closed, not active, and waiting for a command. This is normal because most of application keep the connection even though the business is done, in order to reduce the cost of opening and closing connections. The main purpose of these connections is, reusability. For example, if an application uses connections for retrieving data, the cost of the connection establishment can be minimized if existing one can be used without creating one again. Maintaining a lot of sleeping connection is an overhead? In a way, yes it is, though it is comparatively low. If everything is properly coded, you should not see many sleeping connection. Are we seeing sleeping connection which cannot be reused? Yes, there could be, and we should avoid such situation.
Appendix:
DBCC OPENTRAN helps to identify active transactions that may be preventing log truncation. DBCC OPENTRAN displays information about the oldest active transaction and the oldest distributed and nondistributed replicated transactions, if any, within the transaction log of the specified database.
Use DBCC OPENTRAN to determine whether an open transaction exists within the transaction log. When you use the BACKUP LOG statement, only the inactive part of the log can be truncated; an open transaction can prevent the log from truncating completely.
DBCC OPENTRAN [ ( [ database_name | database_id | 0 ] ) ] { [ WITH TABLERESULTS ] [ , [ NO_INFOMSGS ] ] } ]
参考文档:
How It Works: What is a Sleeping / Awaiting Command Session
SQL Server Sleeping Status and Connection Pooling
Active Request, Sleeping Session (A Month of Activity Monitoring, Part 8 of 30)