![](/img/trans.png)
[英]Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
[英]Will connection leak might Cause Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool?
我收到此錯誤導致我的應用程序停止工作。 Timeout expired.
從池中獲取連接之前經過的超時時間。 這可能是因為所有池連接都在使用中並且達到了最大池大小。
但是我沒有達到我的最大連接池。 我有RDS,在我的監控頁面中,我發現此錯誤發生時連接數為33,默認情況下我的最大值為100。
所以,我想知道這可能是由於我的連接泄漏造成的。
這是我用來連接數據庫的DBLayer
類。
public static DataTable GetDataTable(SqlCommand command, IsolationLevel isolationLevel = IsolationLevel.ReadUncommitted)
{
using (new LoggingStopwatch("Executing SQL " + command.CommandText, command.Parameters))
{
using (var connection = new SqlConnection(connectionString))
using (var dataAdapter = new SqlDataAdapter(command))
{
command.Connection = connection;
command.CommandTimeout = ShopexConfiguration.SqlTimeout;
connection.Open();
var transaction = connection.BeginTransaction(isolationLevel);
command.Transaction = transaction;
try
{
var result = new DataTable();
dataAdapter.Fill(result);
transaction.Commit();
return result;
}
catch
{
try
{
transaction.Rollback();
}
catch (Exception)
{
//
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
}
throw;
}
}
}
}
我只是想知道,這會導致連接泄漏嗎?
我看過這個博客:
任何幫助表示贊賞?
您確定,您的查詢未達到執行超時嗎? SqlConnection.ConnectionTimeout的默認值為15秒
你的connectionString格式中還有一些連接超時值是: “......;連接超時= 10 ......”
我認為你應該在你的finally
語句中關閉你的連接,如下所示:
public static DataTable GetDataTable(SqlCommand command, IsolationLevel isolationLevel = IsolationLevel.ReadUncommitted)
{
using (new LoggingStopwatch("Executing SQL " + command.CommandText, command.Parameters))
{
using (var connection = new SqlConnection(connectionString))
using (var dataAdapter = new SqlDataAdapter(command))
{
command.Connection = connection;
command.CommandTimeout = ShopexConfiguration.SqlTimeout;
connection.Open();
var transaction = connection.BeginTransaction(isolationLevel);
command.Transaction = transaction;
try
{
var result = new DataTable();
dataAdapter.Fill(result);
transaction.Commit();
return result;
}
catch
{
try
{
transaction.Rollback();
}
catch (Exception)
{
//
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
}
finally { connection.Close(); }
throw;
}
}
}
}
也許它可以解決您的問題,因為您在catch語句中回滾事務而不關閉現有連接,您也可以在打開SQL
連接之前使用以下條件:
if (connection.State == ConnectionState.Closed) {
// Open your connection here
connection.Open();
}
// Do your logic here
希望它能幫到你。
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.