我在 asp.net 3.5 中有一个应用程序,数据库是 Sql server 2005。
"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."
有时会出现此错误如何解决此错误..
我尝试 SqlConnection.ClearAllPools();
但这也不起作用。
SqlCommand cmdToExecute = new SqlCommand();
cmdToExecute.CommandText = "dbo.[sp_user_listing]";
cmdToExecute.CommandType = CommandType.StoredProcedure;
DataTable toReturn = new DataTable("courier_user_listing");
SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
// Use base class' connection object
cmdToExecute.Connection = sqMainConnection;
try
{
cmdToExecute.Parameters.Add(new SqlParameter("@suser_name", SqlDbType.VarChar, 250, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _user_name));
if (blnMainConnectionIsCreatedLocal)
{
// Open connection.
sqMainConnection.Open();
}
else
{
if (CPMainConnectionProvider.IsTransactionPending)
{
cmdToExecute.Transaction = CPMainConnectionProvider.CurrentTransaction;
}
}
// Execute query.
adapter.Fill(toReturn);
i32ErrorCode = (Int32)cmdToExecute.Parameters["@iErrorCode"].Value;
if (i32ErrorCode != (int)LLBLError.AllOk)
{
// Throw error.
throw new Exception("Stored Procedure 'sp_courier_user_SelectAll' reported the ErrorCode: " + i32ErrorCode);
}
return toReturn;
}
catch (Exception ex)
{
// some error occured. Bubble it to caller and encapsulate Exception object
throw new Exception("Courier_user::SelectAll::Error occured.", ex);
}
finally
{
if (blnMainConnectionIsCreatedLocal)
{
// Close connection.
sqMainConnection.Close();
}
cmdToExecute.Dispose();
adapter.Dispose();
}
最佳答案
检查数据库中任何长时间运行的查询。
增加你的池大小只会让你的 webapp 活得更长一点(并且可能会变慢很多)
您可以使用 sql server profiler 并根据持续时间/读取进行过滤,以查看哪些查询需要优化。
我也看到你可能保持全局连接?
blnMainConnectionIsCreatedLocal
让 .net 为您进行池化,并使用 using 语句打开/关闭您的连接。
建议:
始终打开和关闭这样的连接,这样 .net 就可以管理您的连接并且您不会耗尽连接:
using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); // do some stuff } //conn disposed
正如我提到的,使用 sql server profiler 检查您的查询,看看您是否可以优化它。在 Web 应用程序中进行包含许多请求的缓慢查询也会导致这些超时。
关于c# - 如何解决最大连接池错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15848239/