c#-为什么长时间在SQL Server上打开sysprocesses?
作者:互联网
我注意到生产数据库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