超时已过期。从池中获取连接之前经过的超时时间。

时间:2014-05-19 07:18:42

标签: c# entity-framework

我正在使用 WebApi AngularJS 开发应用程序。我花了一些时间申请后得到这个例外。我在此应用中使用 EntityFramework

"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."

堆栈跟踪

at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
↵ at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)

6 个答案:

答案 0 :(得分:10)

关闭数据库连接(这非常重要)。

SqlConnection myConnection = new SqlConnection(ConnectionString);
try
{
     conn.Open();
     someCall (myConnection);
}
finally
{
     myConnection.Close();                
}

using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
     myConnection.Open();
     someCall(myConnection);
}

检查数据库连接的用户数以及查询的超时时间。如果您有很长时间执行查询,请检查。

也许,重复的问题:

How can I solve a connection pool problem between ASP.NET and SQL Server?

答案 1 :(得分:3)

我刚遇到同样的问题。我最终使用这样的模式似乎解决了这个问题:

using (SqlConnection con = new SqlConnection(strCon)) 
{
    using (SqlCommand cmd = new SqlCommand(strCmdText, con)) 
    {
        con.Open();
        using (SqlDataReader dr = cmd.ExecuteReader())
         {
              //do stuff;
              dr.Close();
         }
     }
     con.Close();
}

这似乎解决了我的问题。 DataReader.Close()就是这样做的钉子。似乎MS应该改变他们的建议,因为我发现他们的网站上都建议不要使用try { } finally { con.Close(); }模式。我没有明确地尝试这个,因为这个模式在我们整个数据库层中相当普遍,并希望找到更接近的东西。

我希望这有助于某人。

答案 2 :(得分:2)

请尝试以下方法

  1. 始终在finally块中关闭您的连接

  2. 增加连接字符串中的池大小 string connectionString =" Data Source = localhost;初始目录= Northwind;" + "综合安全= SSPI;最小池大小= 10;最大池大小= 100&#34 ;;

                                  or 
    
  3. 根本不使用合并 string connectionString =" Data Source = localhost;初始目录= Northwind;" + "综合安全= SSPI;池=假;&#34 ;;

答案 3 :(得分:1)

建议在SqlConnection和SqlCommand对象周围使用using语句。

请注意,如果你有一个函数在SqlDataReader循环中使用yield return返回IEnumerable,那么推荐的模式。这样,在数据读取器执行之前,将关闭与数据库的连接。

而是将CommandBehavior.CloseConnection参数应用于ExecuteReader调用。

答案 4 :(得分:0)

强制垃圾收集器调用:

var main = function() {

$('.btn').click(function() {
var post = $('.status-box').val();
$('').text(post).prependTo('.posts');
$('.status-box').val('');
$('.counter').text('140');
});
$('.status-box').keyup(function() {
var postLength = $(this).val().length;
var charactersLeft = 140 - postLength;
$('.counter').text(charactersLeft);
if(charactersLeft < 0) {
$('.btn').addClass('disabled');
}
else if(characterLeft == 140) {
$('.btn').addClass('disabled');
}
else { 
 $('.btn').removeClass('disabled');
}
});
$('.btn').addClass('disabled');
};

$(document).ready(main);

答案 5 :(得分:0)

有点老了,很抱歉把它挖出来,但本周在工作中发生了这件事:

@sohail naseer回答,你的连接没有关闭,或者你的数据类没有被正确使用:

如果循环105次,并且在每个循环中声明一个新的数据对象并使用它查询数据库,即使您关闭并处置,您也会创建105连接(从而破坏您的最大允许值100)对象正确,SQL仍需要时间来重新分配与新用户的连接。

相关问题