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

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

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

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


更新:

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

更新:

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


当前回答

这主要是由于应用程序中没有关闭连接。使用“MinPoolSize”和“MaxPoolSize”连接字符串。

其他回答

你也可以尝试一下,解决超时问题:

如果你没有在你的webconfig中添加httpRuntime,在<system中添加。网络>标记

<sytem.web>
     <httpRuntime maxRequestLength="20000" executionTimeout="999999"/>
</system.web>

and

像这样修改你的连接字符串;

 <add name="connstring" connectionString="Data Source=DSourceName;Initial Catalog=DBName;Integrated Security=True;Max Pool Size=50000;Pooling=True;" providerName="System.Data.SqlClient" />

最后使用

    try
    {...} 
    catch
    {...} 
    finaly
    {
     connection.close();
    }

在我的例子中,我没有关闭DataReader对象。

using (SqlCommand dbCmd = new SqlCommand("*StoredProcedureName*"))
using (dbCmd.Connection = new SqlConnection(WebConfigurationAccess.ConnectionString))
{
    dbCmd.CommandType = CommandType.StoredProcedure;

    //Add parametres
    dbCmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int)).Value = ID;

    .....
    .....

    dbCmd.Connection.Open();
    var dr = dbCmd.ExecuteReader(); //created a Data reader here
    dr.Close();    //gotta close the data reader
    //dbCmd.Connection.Close(); //don't need this as 'using' statement should take care of this in its implicit dispose method.
}

除了公布的解决方案。

在处理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

我也有同样的问题,想要分享帮助我找到源头的东西: 将应用程序名称添加到连接字符串中,然后监视到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>'

您需要告诉数据库服务器在卸载页面时关闭连接。

protected void Page_Unload(object sender, EventArgs e) {
    if (qryCnn != null) 
        qryCnn.Close();
}