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

如果应用程序遇到了下面错误信息,那么意味着连接池(connection pool)的连接数量由于一些原因导致其超过了max pool size参数的限制。

 

英文错误信息:

 

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

 

中文错误信息:

 

超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

 

 

 

在介绍这个错误前,我们必须搞清楚一些概念,后续再展开分析这个问题出现的原因,以及出现后如何解决问题。

 

 

 

连接池(connection pool)

 

对于共享资源,有一个很著名的设计模式:资源池(resource pool)。该模式正是为解决资源频繁分配、释放所造成的问题。数据库连接池(connection pool)的基本思想就是为数据库连接建立一个缓冲池。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需要从缓冲池中取出一个连接,使用完毕后再放回去。数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个。避免重复多次的打开数据库连接而造成的性能的下降问题和系统资源的浪费问题。

 

 

连接池相关参数

 

由于访问数据库的驱动很多,不同驱动的连接池参数等可能有所差异,具体以实际情况为准,我们以ado.net为例,来分析一个连接数据库的连接配置,providername=”system.data.sqlclient” 这里没有设置max pool sizepooing等参数,那么其实都是取其对应的默认值。其中pooling参数默认情况下为true,max pool size值为100

 

<add name="xxxx" connectionstring="data source = 192.168.xxx.xxx;initial catalog=xxx;user id=xxxx;password=xxxx;multipleactiveresultsets=true;" providername="system.data.sqlclient" /> 

 

 

keyword

default

description

max pool size

100

the maximum number of connections that are allowed in the pool.

valid values are greater than or equal to 1. values that are less than min pool size generate an error.

min pool size

0

the minimum number of connections that are allowed in the pool.

valid values are greater than or equal to 0. zero (0) in this field means no minimum connections are initially opened.

values that are greater than max pool size generate an error.

pooling

‘true’

when the value of this key is set to true, any newly created connection will be added to the pool when closed by the application. in a next attempt to open the same connection, that connection will be drawn from the pool.

connections are considered the same if they have the same connection string. different connections have different connection strings.

the value of this key can be “true”, “false”, “yes”, or “no”.

poolblockingperiod

auto

sets the blocking period behavior for a connection pool. see poolblockingperiod property for details.

 

keyword

default

description

max pool size

100

池中允许的最大连接数。

有效值大于或等于1 小于最小池大小的值会生成错误。

min pool size

0

池中允许的最小连接数。

有效值大于或等于0 此字段中的零(0)表示最初没有打开最小值连接。

大于最大池大小的值会生成错误。

pooling

‘true’

如果此项的值设置为 true,则在应用程序关闭时,将向池中添加任何新创建的连接。 在下一次尝试打开相同的连接时,该连接将从池中提取。

 

如果连接具有相同的连接字符串,则将其视为相同。 不同连接具有不同的连接字符串。

 

此键的值可以为 “true”“false”“yes” “no”

poolblockingperiod

auto

设置连接池的阻塞期行为。 有关详细信息,请参阅 poolblockingperiod 属性。

 

 

 

 

 

 

 

错误出现的原因:

 

 

if we try to obtain connections more than max pool size, then ado.net waits for connection timeout for the connection from the pool. if even after that connection is not available, we get the following exception.

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”

 

the timeout expired. the timeout expired 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

 

when you open an sql connection object, it always takes from an available pool of connections. when you close the connection, asp.net will release the connection to the pool of connections so that next connection object can use it.if you open connections with out closing them and when the pool reaches maximum connections, it will throw the specified error. make sure you are not opening connection inside loop, if you open connection make sure you are closing it immedietly after you execute the query.

 

错误的原因其实就是连接池中的连接被用完了,后面的请求建立数据库连接时,连接池中没有可用的资源(连接),那么就分配到等待连接池分配的队列中,而其等待的时间超过了参数connection timeout”的值,就抛出这个异常信息。

 

 

错误的解决方案:

 

 

 

1:业务量突然暴增,当前并发请求连接数据的数量超过了连接池的最大连接数(默认情况下,最大连接数是100),出现这种情况时,必须调整数据库连接字符串参数max pool size值为一个合适的值。这种情况一般较少见。

 

 

 

connection pooling and the “timeout expired” exception faq这篇文章中有代码模拟这种情况,这里就不做展开了。有兴趣的自己模拟一下即可。

 

 

  sqlconnection[] connectionarray = new sqlconnection[101];
      for (int i = 0; i <= 100; i++)
      {
                  connectionarray[i] = new sqlconnection("server=.\\sqlexpress ;integrated security=sspi;connection timeout=5");
                  connectionarray[i].open();
      }

 

 

 

2 leaking connections问题导致。

 

 

其实这里个人理解为数据库连接没有正常关闭。有些是代码逻辑问题导致,有些是没有正确处理异常问题。

 

 

案例1

 

这个案例来自connection pooling and the “timeout expired” exception faq,它模拟的是一种逻辑异常问题。如果开发人员这样写代码的话,那么即使出现异常,但是数据库连接永远不会释放(sqlconnection1.close()永远不会执行)

 

using system;
using system.data;
using system.data.sqlclient;
 
public class repro
{
    public static int main(string[] args)
    {
        repro repro = new repro();
        for (int i = 0; i <= 5000; i++)
        {
            try{ console.write(i+" ");    repro.leakconnections(); }
            catch (sqlexception){}
        }
 
        return 1;
    }
    public void leakconnections()
    {    
        sqlconnection sqlconnection1 = new sqlconnection("server=.\\sqlexpress ;integrated security=sspi;connection timeout=5");
        sqlconnection1.open();
        sqlcommand sqlcommand1 = sqlconnection1.createcommand();
        sqlcommand1.commandtext = "raiserror ('this is a fake exception', 17,1)";
        sqlcommand1.executenonquery();  //this throws a sqlexception every time it is called.
        sqlconnection1.close(); //we are calling connection close, and we are still leaking connections (see above comment for explanation)
    }
}

解决方法:

 

    我们要保证每次调用连接的同时都在使用过后通过close()dispose()对其执行了关闭.最简单的办法就是使用using,类似下面这样的代码

 

public void doesnotleakconnections()
      {
                  using (sqlconnection sqlconnection1 = new sqlconnection("server=.\\sqlexpress ;integrated security=sspi;connection timeout=5")) {
                              sqlconnection1.open();
                              sqlcommand sqlcommand1 = sqlconnection1.createcommand();
                              sqlcommand1.commandtext = "raiserror ('this is a fake exception', 17,1)";
                              sqlcommand1.executenonquery(); //this throws a sqlexception every time it is called.
                              sqlconnection1.close(); //still never gets called.
                  } // here sqlconnection1.dispose is _guaranteed_
      }

 

案例2

 

案例2来自官方文档timeout expired. the timeout period elapsed prior to obtaining a connection from the pool,其实我也见过几起类似这样的案例。有现成的例子,就没必要自己构造这样的案例。

 

static void main(string[] args)
 
        {
 
            string connstring = @"data source=<your server>;initial catalog=northwind;integrated security=true; max pool size=20; connection timeout=10";
 
            try
 
            {
 
                for (int i = 0; i < 50; i++)
 
                {
 
                    // create connection, command and open the connection
 
                    sqlconnection sc = new sqlconnection(connstring);
 
                    sqlcommand scmd = new sqlcommand("select * from shippers", sc);
 
                    sc.open();
 
                    // print info
 
                    console.writeline("connections open: {0}", i.tostring());
 
                    // this will cause the error to show.
 
                    sqldatareader sdr = scmd.executereader();
 
                    sdr.close();
 
 
 
                    // replacing the two rows above with these will remove the error
 
                    //sqldatareader sdr = scmd.executereader(system.data.commandbehavior.closeconnection);
 
                    //sdr.close();
 
 
 
                    // -- or --
 
                    // explicity close the connection
 
                    //sc.close();
 
                }
 
 
 
                    // -- or --
 
                    // run all in a using statement (in this case, replace the whole for loop with the loop below.).
 
 
 
                    //for (int i = 0; i < 50; i++)
 
                    //{
 
                    //    using (sqlconnection sc = new sqlconnection(connstring))
 
                    //    {
 
                    //        sqlcommand scmd = new sqlcommand("select * from shippers", sc);
 
                    //        sc.open();
 
                    //        console.writeline("conns opened " + i.tostring());
 
                    //        sqldatareader sdr = scmd.executereader();
 
                    //        sdr.close();
 
                    //    }
 
                    //}
 
 
 
            }
 
            catch (exception e)
 
            {
 
                console.writeline(e);
 
            }
 
        }

 

3:通过visual studio中的sql debugging 来打开或关闭连接的, 这个参考connection pooling and the “timeout expired” exception faq的详细介绍。个人倒是没有遇到过这种情况。

 

 

 

个人的一些经验体会,遇到这些问题后,我们首先应该监控数据库的连接数量信息,类似下面这样的sql,根据实际情况来调整(例如,有些程序的iis部署在某个应用服务器上,我们通过hostname基本上就能定位)

 

 

select  hostname ,
        count(*) as connection_sum
from    sys.sysprocesses
group by hostname
order by 2 desc;
 
 
select  loginame ,
        count(*) as connection_sum
from    sys.sysprocesses
group by loginame
order by 2 desc;

 

 

然后要跟开发人员协作检查连接数据库的配置信息(连接字符串设置),例如max pool size的大小设置,然后就是最麻烦的问题,怎么定位到root cause呢? 这就需要开发人员去检查了。已经脱离了dba的掌控了。个人经验(不做开发多年了,经验都过时了),如果突然出现这个问题,并且有源代码版本控制管理,最好找出最近的修改部分,进行细致的检查验证,基本上就能定位到问题根源了。但是能否做到细致检查,因人而异。这个往往最难掌控,跟个人的态度、经验、能力有很大关系。

 

 

 

 

参考资料:

 

 

https://blogs.msdn.microsoft.com/spike/2008/08/25/timeout-expired-the-timeout-period-elapsed-prior-to-obtaining-a-connection-from-the-pool/

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐