简体   繁体   English

超时已过。 在从池中获取连接之前超时时间已过。

[英]Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.

I am working on an application using WebApi and AngularJS .我正在使用WebApiAngularJS开发一个应用程序。 I am getting this exception after spending sometime to application.花了一些时间申请后,我得到了这个例外。 I am using EntityFramework in this app.我在这个应用程序中使用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."

Stack Trace堆栈跟踪

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)

Close your database connections (it's really important).关闭您的数据库连接(这非常重要)。

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

or或者

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

Check how many users are connected to your database and the time out for querys.检查有多少用户连接到您的数据库以及查询超时。 Check too if you have long time executing querys.如果您有很长时间执行查询,也请检查。

Perhaps, duplicate question:也许,重复的问题:

How can I solve a connection pool problem between ASP.NET and SQL Server? 如何解决 ASP.NET 和 SQL Server 之间的连接池问题?

When does Entity Framework open and close Database Connections? Entity Framework 何时打开和关闭数据库连接?

I just experienced the same problem.我刚刚遇到了同样的问题。 I ended up using a pattern like this which seemed to fix the issue:我最终使用了这样的模式,这似乎解决了这个问题:

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();
}

This seemed to fix my problem.这似乎解决了我的问题。 DataReader.Close() was the nail that did it. DataReader.Close()是做到这一点的关键。 It seems like MS should change their recommendation since I've found it all over their site suggesting not to use the try { } finally { con.Close(); }似乎 MS 应该改变他们的建议,因为我在他们的网站上发现它建议不要使用try { } finally { con.Close(); } try { } finally { con.Close(); } pattern. try { } finally { con.Close(); }模式。 I didn't try this explicitly, since the pattern is fairly pervasive throughout our entire db layer and wanted to find something closer.我没有明确地尝试这个,因为该模式在我们整个 db 层中相当普遍,并且想要找到更接近的东西。

I hope this helps someone.我希望这可以帮助别人。

Please try the following things请尝试以下事情

  1. Always close your connection in the finally block始终在 finally 块中关闭您的连接

  2. Increase pool size like in your connection string string connectionString = "Data Source=localhost; Initial Catalog=Northwind;"像在连接字符串 string connectionString = "Data Source=localhost; Initial Catalog=Northwind;" 中一样增加池大小+ "Integrated Security=SSPI; Min Pool Size=10; Max Pool Size=100"; +“集成安全=SSPI;最小池大小=10;最大池大小=100”;

     or
  3. Don't use pooling at all string connectionString = "Data Source=localhost; Initial Catalog=Northwind;"根本不要使用池 string connectionString = "Data Source=localhost; Initial Catalog=Northwind;" + "Integrated Security=SSPI; Pooling=false;"; + "集成安全性 = SSPI;池化 = false;";

It was suggested to use the using statement around SqlConnection and SqlCommand objects.建议在 SqlConnection 和 SqlCommand 对象周围使用 using 语句。

Please note that if you have a function returning an IEnumerable with the use of yield return in a SqlDataReader loop, this is not the recommended pattern.请注意,如果您在 SqlDataReader 循环中使用 yield return 返回 IEnumerable 的函数,则这不是推荐的模式。 Doing so, the connection to the database will be closed before the data reader will be executed.这样做,与数据库的连接将在执行数据读取器之前关闭。

Instead, apply the CommandBehavior.CloseConnection parameter to the ExecuteReader call.相反,将 CommandBehavior.CloseConnection 参数应用于 ExecuteReader 调用。

强制垃圾收集器调用:

System.GC.Collect()

a bit old, sorry for diggin this out, but it happened to us this week at work :有点旧,很抱歉将其挖掘出来,但这周发生在我们工作中:

either as @sohail naseer answered, your connections aren't closed, or you have a data class that isnt used correctly :正如@sohail naseer 回答的那样,您的连接没有关闭,或者您的数据类未正确使用:

If you loop 105 times, and in each loop you declare a new data object and you query the DB with it, you'll have created 105 connection (thus busting your 100 max allowed ) even if you close and dispose the objects correctly, SQL still need time to reassigned that connection to a new user.如果循环 105 次,并且在每个循环中声明一个新的数据对象并使用它查询数据库,即使您正确关闭和处理对象,您也将创建 105 个连接(从而破坏了您的 100 个最大允许数),SQL仍然需要时间将该连接重新分配给新用户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 超时已过。 在从池中获取连接之前超时时间已过。 - Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. 让超时过期。 从池中获取连接之前经过的超时时间。 。例外 - Getting Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. .exception 连接泄漏是否会导致Timeout过期。 从池中获取连接之前是否已经过了超时时间? - Will connection leak might Cause Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool? 获取超时已过期。 从池中获取连接之前经过的超时时间 - Getting Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool 获取异常超时已过期。 从池中获取连接之前经过的超时时间 - Getting exception Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool 超时时间已到。 获得连接之前经过的超时时间第一个连接 - Timeout expired. The timeout period elapsed prior to obtaining a connection First connection ASP - 从池中获取连接之前经过的超时时间 - ASP - the timeout period elapsed prior to obtaining a connection from the pool 从池中获取连接之前经过的超时时间 - The timeout period elapsed prior to obtaining a connection from pool 如何修复“从池中获取连接之前已过超时时间” - How to fix “Timeout period elapsed prior to obtaining a connection from the pool” 从池中获取连接之前经过的超时时间 - The timeout period elapsed prior to obtaining a connection from the pool
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM