简体   繁体   English

如何避免MSSQL中的“休眠”进程?

[英]How do I avoid “sleeping” processes in MSSQL?

Currently I'm experiencing alot of issues with Server Processes (Seen from sp_who2) that is "sleeping" instead of just, finishing (being removed), when I connection to my MSSQL Database, calls a Stored Procedure, get some data and then closing the connection. 目前我遇到了很多服务器进程问题(从sp_who2看到),它正在“休眠”而不仅仅是,完成(被删除),当我连接到我的MSSQL数据库,调用存储过程,获取一些数据然后关闭连接。

What's the best way in C#/.NET to connect to a MSSQL database, call a Stored Procedure, retrieve data from the Stored Procedure (Data Reader) and then close the connection? C#/ .NET连接到MSSQL数据库,调用存储过程,从存储过程(数据读取器)检索数据然后关闭连接的最佳方法是什么?

Is there a way to close/dispose so that the "sleeping" processes gets killed? 有没有办法关闭/处置,以便“睡眠”进程被杀死?

Does it have something to do with me creating new SqlConnections, opening them and closing them all the time? 它与我创建新的SqlConnections,打开它们并一直关闭它们有什么关系吗?

My flow is as follows: 我的流程如下:

A request occur: 发出请求:

  1. I create a new SqlConnection Instance that connects to my MSSQL Database. 我创建了一个连接到我的MSSQL数据库的新SqlConnection实例。
  2. I call a Stored Procedure, retrieve the data and presents it to the user. 我调用存储过程,检索数据并将其呈现给用户。
  3. I close the Connection with .Close(); 我用.Close();关闭了Connection .Close();
  4. I repeat all these steps for each request. 我为每个请求重复所有这些步骤。 Requests happen once every 5-10 seconds. 请求每5-10秒发生一次。 (Sometimes slower, sometimes faster) (有时更慢,有时更快)

I'm sorry If this question is missing some details, but I hope this is enough to get a some what helpful answer. 对不起如果这个问题缺少一些细节,但我希望这足以得到一些有用的答案。

Thanks! 谢谢!

you need to use SET XACT_ABORT ON or add some client rollback code 您需要使用SET XACT_ABORT ON或添加一些客户端回滚代码

When a client timeout event occurs (.net CommandTimeout for example), the client sends an "ABORT" to SQL Server. 当发生客户端超时事件(例如.net CommandTimeout)时,客户端向SQL Server发送“ABORT”。 SQL Server then simply abandons the query processing. 然后SQL Server简单地放弃查询处理。 No transaction is rolled back, no locks are released. 没有回滚事务,也没有释放锁。

Now, the connection is returned to the connection pool, so it isn't closed on SQL Server. 现在,连接将返回到连接池,因此它不会在SQL Server上关闭。 If this ever happens (via KILL or client reboot etc) then the transactions+locks will be cleared. 如果发生这种情况(通过KILL或客户端重启等),那么将清除事务+锁。 Note that sp_reset_connection won't or doesn't clear them, even though it is advertised to do so 请注意,sp_reset_connection不会或不会清除它们,即使它已被通告也是如此

This detritus from the abort will block other processes. 来自中止的碎屑将阻止其他过程。

The way to make SQL Server clear transactions+locks on client timeout (strictly, ABORT events) is to use SET XACT_ABORT ON. 在客户端超时(严格地说,ABORT事件)上使SQL Server清除事务+锁定的方法是使用SET XACT_ABORT ON。

You can verify this be opening 2 query windows in SSMS: 您可以验证这是在SSMS中打开2个查询窗口:

Window 1: 窗口1:

In menu Query..Query Options set a timeout of 5 seconds then run this 在菜单Query..Query Options中设置超时5秒然后运行它

BEGIN TRAN
UPDATE sometable WITH (TABLOCKX) SET foo = foo WHERE 1 = 0;
WAITFOR DELAY '00:00:10' -- just has to be longer then timeout

Window 2, this will wait forever (or hit your timeout) 窗口2,这将永远等待(或达到你的超时)

SELECT * FROM sometable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM