最近几天,我们经常在网站上看到这样的错误信息:

“超时过期。超时时间 在获取 来自池的连接。这可能 已经发生是因为全部池化了吗 连接正在使用,马克斯泳池 规模达到了。”

我们已经有一段时间没有更改代码中的任何内容了。我修改了代码以检查未关闭的打开连接,但发现一切正常。

我怎么解决这个问题? 我需要编辑这个池吗? 如何编辑此池的最大连接数? 高流量网站的推荐值是多少?


更新:

我需要在IIS中编辑一些东西吗?

更新:

我发现活动连接的数量在15到31之间,我发现在SQL server中配置的最大允许连接数超过3200个连接,是31太多了还是我应该在ASP中编辑一些东西。网络配置?


当前回答

您已经泄漏了代码上的连接。您可以尝试使用using来证明您正在关闭它们。

using (SqlConnection sqlconnection1 = new SqlConnection(“Server=.\\SQLEXPRESS ;Integrated security=sspi;connection timeout=5”)) 
{
    sqlconnection1.Open();
    SqlCommand sqlcommand1 = sqlconnection1.CreateCommand();
    sqlcommand1.CommandText = “raiserror (‘This is a fake exception’, 17,1)”;
    sqlcommand1.ExecuteNonQuery();  //this throws a SqlException every time it is called.
    sqlconnection1.Close(); //Still never gets called.
} // Here sqlconnection1.Dispose is _guaranteed_

https://blogs.msdn.microsoft.com/angelsb/2004/08/25/connection-pooling-and-the-timeout-expired-exception-faq/

其他回答

除非您的使用量增加了很多,否则不太可能只是有工作积压。在我看来,最有可能的选择是某些东西正在使用连接,并且没有及时释放它们。你确定你在所有情况下都使用了using吗?或者(通过任何机制)释放连接?

如果您正在处理复杂的遗留代码,只需使用(..){..}是不可能的-正如我所做的那样-您可能想要查看我在这个SO问题中发布的代码片段,以确定当连接可能泄漏(在设置超时后未关闭)时连接创建的调用堆栈。这使得发现泄漏的原因相当容易。

我也有同样的问题,想要分享帮助我找到源头的东西: 将应用程序名称添加到连接字符串中,然后监视到SQL Server的打开连接

select st.text,
    es.*, 
    ec.*
from sys.dm_exec_sessions as es
    inner join sys.dm_exec_connections as ec on es.session_id = ec.session_id
    cross apply sys.dm_exec_sql_text(ec.most_recent_sql_handle) st
where es.program_name = '<your app name here>'

对于我来说,使用实体框架,我错过了可以简化using语句的智能感知建议。 我错误地简化了using语句

ie

using (var db = new MyDbContext()){}

化简为

using var db = new MyDbContext();

not

var db = new MyDbContext();

是的,有一种方法可以改变配置。如果你在一个专用的服务器上,只是需要更多的SQL连接,你可以在两个连接字符串中更新“最大池大小”条目,按照下面的说明:

Log into your server using Remote Desktop Open My Computer (Windows - E) and go to C:\inetpub\vhosts[domain]\httpdocs Double click on the web.config file. This may just be listed as web if the file structure is set to hide extensions. This will open up Visual Basic or similar editor. Find your Connection Strings, these will look similar to the examples below : "add name="SiteSqlServer" connectionString="server=(local);database=dbname;uid=dbuser;pwd=dbpassword;pooling=true;connection lifetime=120;max pool size=25;""

5.将最大池大小=X值更改为所需的池大小。

保存并关闭你的网页。配置文件。