由于连接未关闭而导致 ASP.net 超时

发布于 2024-11-15 16:13:06 字数 553 浏览 3 评论 0原文

超时已过。从池中获取连接之前超时时间已过。发生这种情况的原因可能是所有池连接都在使用中并且已达到最大池大小。

这是我很久以前开发的第一个 ASP.net 站点,它的很多页面的顶部(以及很多方法中)都有这段代码。

cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
cn.Open();

很多页面都没有:

cn.Close();

而且没有一个页面执行 using(SqlConnection...) (尽管所有数据读取器都在 using 块中)。

第一个问题是,错误的主要原因是什么?

第二个问题是,解决此重构/手动搜索未关闭连接的最佳方法是?我知道这是一个丑陋的 hack,但是重构所花费的时间会很大,但是我们可以设置一个计划任务,每天凌晨 3 点回收一次连接池什么的吗?

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.

This is the first ASP.net site I developed a long time ago, it has this code at the top of a lot of pages (and in a lot of methods).

cn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
cn.Open();

A lot of pages don't have:

cn.Close();

Also none of the pages do using(SqlConnection...), (although all the data readers are in using blocks).

First question is, is the the primary candiate for the error?

Second question is, is the best way to solve this refactoring/manually searching for unclosed connections? I know it's an ugly hack, but the time spent refactoring will be huge, but can we set a scheduled task to recycle the connection pool once a day at 3am or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作业与我同在 2024-11-22 16:13:06

是的,这是错误的主要原因。目前,其中许多连接将等待下一次 GC 来重新池化底层连接。您将很快耗尽池(和数据库连接)。

重构此问题的最佳方法是添加缺少的 using,以便确定连接的作用域。就我个人而言,我还将其重构为单一方法,即

using(var cn = SomeUtilityClass.GetOpenConnection())
{...}

关闭阅读器几乎没有什么作用,除非将阅读器标记为 关闭连接;让数据读取器关闭连接(通过某种行为)有点是可行的,但它假设您尽可能消耗读取器 - 它在错误情况下不一定表现良好。

Yes, that is the primary cause of the error. Currently, many of those connections will wait until the next GC to re-pool the underlying connection. You will exhaust the pool (and database connections) pretty quickly.

The best way of refactoring this is to add the missing using, such that the connection is scoped. Personally I'd also refactor that to a single method, i.e.

using(var cn = SomeUtilityClass.GetOpenConnection())
{...}

Closing the reader does little unless the reader is marked to close the connection; and having the data-reader close the connection (via a behaviour) sort of works, but it assumes you get as far as consuming the reader - it won't necessarily behave well in error scenarios.

七婞 2024-11-22 16:13:06

即使我在曾经工作过的应用程序中也遇到过这个错误。我发现的问题是相同的 - 没有使用语句,也没有千钧一发。

我建议您在整个项目中搜索 SqlConnection,然后在 using 语句中包含 SqlConnection、SqlCommand 和 SqlDataAdapter,并在 sqlconnection using 语句中执行 connection.close。与此一起在配置文件中增加连接字符串中的连接超时。您还可以使用 SqlCommand 的 CommandTimeout 属性。

Even I have encountered this error in the application that I once worked on. The problem that I identified was the same - no using statements and no close calls.

I would advise you to search the whole project for SqlConnection and then include the SqlConnection, SqlCommand and SqlDataAdapter in using statements and also do a connection.close within the sqlconnection using statement. Together with this in the config file increase the timeout of the connection within the connection string. You can also you CommandTimeout property of SqlCommand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文