Sleeping Session

SQL Server 2012中,Sleeping Session表示当前没有运行任何Request,

MSDN 文档的定义:Sleeping - Currently running no requests

如果一个Session 没有运行任何Request,那么该 Session 为什么不结束,而要保持Sleeping Status?

一,Sleeping Session虽然没有运行任何Request,但是,和SQL Server的Connection并没有断开,可能原因主要有两个:

  1. Session中存在 Transaction 没有commit;
  2. 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 ] ]
    }
] 

参考文档:

DBCC OPENTRAN (Transact-SQL)

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)

时间: 2024-10-03 14:55:51

Sleeping Session的相关文章

数据库的创建和文件的修改

在SQL Server中,使用Create Database创建数据库,使用Alter Database命令,能够修改数据库的数据文件和日志文件. 一,创建数据库 1,在创建数据库时,最佳实践是: 创建一个或多个文件组,并设置默认文件组 每个文件组中的数据文件和CPU的内核数据相同,将文件均匀分布在不同的物理硬盘上,使IO均匀分布在不同的物理磁盘上: 数据文件的初始大小,文件增长和最大大小保持一致,这样,能够使每个文件的IO次数相对均匀: 日志文件分配在性能最好的物理硬盘上,写日志的性能直接影响

DB1:数据库的创建和文件的修改

在SQL Server中,使用Create Database创建数据库,使用Alter Database命令,能够修改数据库的数据文件和日志文件. 一,创建数据库 1,在创建数据库时,最佳实践是: 创建一个或多个文件组,并设置默认文件组 每个文件组中的数据文件和CPU的内核数据相同,将文件均匀分布在不同的物理硬盘上,使IO均匀分布在不同的物理磁盘上: 数据文件的初始大小,文件增长和最大大小保持一致,这样,能够使每个文件的IO次数相对均匀: 日志文件分配在性能最好的物理硬盘上,写日志的性能直接影响

Juniper防火墙 session 过高问题

问题:session 100% 日志报错: Session utilization has reached 43257, which is 90% of the system capacity! session 连接过高 解决方法: 1.通过telnet 或 consol的方法登录到防火墙 2.使用get session 查看总的session会话数,如果大于300 一般属于不正常情况 alloc 48000/max 48064, alloc failed 2682725821, mcast a

系统Session的WaitType

在一个时间点,从Server上查询到的系统Session的Wait type 截图 1,CHECKPOINT_QUEUE Occurs while the checkpoint task is waiting for the next checkpoint request. Description:This wait type is when the background checkpoint process is idle waiting to be signaled that a new c

Tomcat集群session复制,httpd/nginx反代Tomcat集群

   一个大型站点都会涉及到动态应用,动态应用都需要做会话保持,常见的会话保持方式就三种,一是session stick,二是session replication,三是session share,对于小型规模的tomcat集群,大多者会采用session replication方式,但阅读官方文档也好,查询大牛博客也罢,发现均有不准确之处,所以亲测成功实现之后得出如下文档,还望高人指点. 实验环境: 操作系统:CentOS 7.2 tomcat版本:tomcat-7.0.54(yum安装方式)

Basic Concepts2:Session

sys.dm_exec_sessions Returns one row per authenticated session on SQL Server. sys.dm_exec_sessions is a server-scope view that shows information about all active user connections and internal tasks. This information includes client version, client pr

javaWeb核心技术第八篇之Cookie和Session

会话技术: 会话是什么? 浏览器和服务器交互,浏览器打开网页访问服务器,会话开始,正常交互. 浏览器关闭,会话结束. 会话能干什么? 会话可以共享数据. Cookie和session将数据保存在不同的位置 进行数据共享 Cookie入门案例 1.创建一个cookie对象 a. Cookie cookie = new Cookie(String name,String value); 2.响应给浏览器 a.Response.addCookie(cookie) 3.再次请求时需要获得cookie a

COOKIE+SESSION

cookie的缺点: 因为cookie保存在浏览器上,所以安全性低可控性比较差,只能存放字符串大多数的浏览器对cookie有4K的限制. 下面是cookie在浏览器和服务器中请求与响应的过程: 1.    COOKIE的工作原理 cookie过程描述 网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密) 用户第一次访问你的网站->在服务器端会将用户的信息设置为cookie(可以理解为制造饼干过程)->通过http协议发送给用户(浏览器),在用户端,coo

Nginx做负载均衡时session共享问题详解

用nginx做负载均衡时,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,就会出现很多问题,比如说最常见的登录状态. 再者Nginx连接Memcached集群时,Nignx的请求从memcached服务器中根据key获得了value则直接返回value,如果没有获得到value则去MySQL中查询再返回. location / { set $memcached_key "$request_uri"; #设置请求memcached服务器的key memca