ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

c#-为什么长时间在SQL Server上打开sysprocesses?

2019-10-26 18:08:49  阅读:361  来源: 互联网

标签:connection-pooling nhibernate c sql-server


我注意到生产数据库SQL Server上有许多数据库连接处于打开状态,有些连接时间很长.其中许多都有公开交易.如查询中所示:

select * from sysprocesses where open_tran>0

它们的状态为“ sleeping”,cmd =“ AWAITING COMMAND”.

使用NHIBERNATE打开连接,如下所示:

For<ISessionFactory>().Singleton()
    .Use(() => DataContext.GetSessionFactory());

For<ISession>().Transient()
  .Use(context => context.GetInstance<ISessionFactory>().OpenSession());

一些会话使用事务范围:
     _transactionScope = new TransactionScope();

有些创建事务:

_uncommittedTransaction = SessionUncommittedWrapper.Session.BeginTransaction(IsolationLevel.ReadUncommitted);

之后将transaction和/或transactionScope丢弃.

为什么连接仍然打开?如果什么都没有被阻止,这是一个问题吗?

解决方法:

They are in status = “sleeping”, cmd = “AWAITING COMMAND”.

当您在sysprocesses中看到一行状态为sleeping的行时,表示此连接处于活动状态,但未使用..由于SQLServer使用connection Pooling mechanism,也会发生这种情况.

等待命令可以通过以下示例进行说明.

会话1:开始此交易

begin tran
insert into sometable
select some columns

现在,当您在sys.processes / session DMV中检查session1的状态时,您可以看到它是Awaiting command..since,您没有提交它.

等待命令的另一个原因可能是当您开始事务并等待用户输入时,例如

begin tran
update t
set t1.a=<<some input from user>>--waiting
where t1.a=oldvalue
commit

And is it a problem, if nothing is being blocked?

只要您看到,什么都没有被阻塞,睡眠和会话没有任何锁,并且您没有看到任何打开的事务,则无需担心.这由Bob Dorr in this article解释.

The question usually arises around a session that is holding locks and its state is sleeping / awaiting command. If the client has an open transaction and the client did not submit a commit or rollback command the state is sleeping / awaiting command. I see this quite often with a procedure that times out.

Create proc myProc
As
Begin tran
Update authors ….
 Waitfor delay ’10:00:00’   — time out will occur here  (simulates long workload)
 rollback
go

When run from the client with a 30 second query timeout the transaction will remain open because the client indicated it wanted to ‘cancel execution’ and do no further processing.

To get automatic rollback in this situation transaction abort must be enabled. You now have an open transaction with a SPID sleeping/awaiting command.
The situation can be caused by many other variations but it is always a situation where the SQL Server is waiting for the next command from the client

标签:connection-pooling,nhibernate,c,sql-server
来源: https://codeday.me/bug/20191026/1938189.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有