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

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

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

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


更新:

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

更新:

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


当前回答

您是否检查了未关闭和响应的数据阅读器。在关闭连接或数据读取器之前重定向。如果在重定向之前不关闭连接,则连接将保持打开状态。

其他回答

对于EntityFramework 这可能是连接过载的情况。你只需要修复连接字符串添加这个字段到连接字符串:

=> Max Pool Size=200

例子:

<add name="DataProvider" connectionString="Data Source=.;Initial Catalog=dbname;User ID=dbuser;Password=dbpassword;Max Pool Size=200" providerName="System.Data.SqlClient" />

除了公布的解决方案。

在处理1000页遗留代码时,每个代码都多次调用公共GetRS,这里有另一种解决问题的方法:

在现有的公共DLL中,我们添加了CommandBehavior。CloseConnection选择:

static public IDataReader GetRS(String Sql)
{
    SqlConnection dbconn = new SqlConnection(DB.GetDBConn());
    dbconn.Open();
    SqlCommand cmd = new SqlCommand(Sql, dbconn);
    return cmd.ExecuteReader(CommandBehavior.CloseConnection);   
}

然后,在每个页面中,只要关闭数据读取器,连接也会自动关闭,从而防止连接泄漏。

IDataReader rs = CommonDLL.GetRS("select * from table");
while (rs.Read())
{
    // do something
}
rs.Close();   // this also closes the connection

在我的情况下,我有无限循环(从一个get属性试图从数据库中获取值),不断打开数百个Sql连接。

要重现这个问题,试试这个:

while (true)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        someCall(connection);
    }
}

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

是的,有一种方法可以改变配置。如果你在一个专用的服务器上,只是需要更多的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值更改为所需的池大小。

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