[英]Connection pool issues on shared hosting with proper disposal
I know that there is a similar question like this on Stack Overflow, but it doesn't quite fit what I have tried. 我知道在Stack Overflow上也有类似的问题,但这与我尝试过的不完全相同。 I believe the reason for this problem is different, although the end-result is the same.
我相信导致此问题的原因有所不同,尽管最终结果是相同的。
Whenever my site has been running for about two or three days, I get the following error (which is obviously caused by either connections/queries hanging, or not being disposed properly). 每当我的网站运行了大约两三天时,都会出现以下错误(这显然是由于连接/查询挂起或处置不当引起的)。
Timeout expired.
超时时间已到。 The timeout period elapsed prior to obtaining a connection from the pool.
从池中获取连接之前已经过超时时间。 This may have occurred because all pooled connections were in use and max pool size was reached.
这可能是因为所有池化连接都在使用中,并且达到了最大池大小。
At first, I did some investigation and realized that using using
, using a finalize
call and other solutions weren't enough. 最初,我进行了一些调查,并意识到仅使用
using
, finalize
调用和其他解决方案是不够的。 The error would still eventually occur. 该错误最终仍将发生。
So I decided to figure out wether or not the Server Explorer (which I use often) in Visual Studio leaks connections. 因此,我决定弄清楚Visual Studio中的服务器浏览器(我经常使用)是否泄漏连接。 I had already changed the maximum execution and connection timeout to 3 seconds, and had reduced the pool size to 10 connections, so the Server Explorer could be the cause.
我已经将最大执行和连接超时更改为3秒,并将池的大小减小为10个连接,因此可能是服务器资源管理器引起的。 No luck!
没运气!
So what do I try now? 那我现在该怎么办?
Here's the code I solved the problem with. 这是我解决问题的代码。 When connecting, catch the exception and clear all pools, and then try again.
连接时,捕获异常并清除所有池,然后重试。
_connection =
new SqlConnection(connectionString);
try
{
_connection.Open();
}
catch (InvalidOperationException)
{
SqlConnection.ClearAllPools();
_connection.Open();
}
Also, when disposing the connection, make sure to clear the pools. 另外,在布置连接时,请确保清除池。
if (_connection != null)
{
SqlConnection.ClearAllPools();
_connection.Close();
_connection.Dispose();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.