最近在做多线程处理数据库的程序时,这个程序总是会报如下错误:
超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
System.Data
在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
在 System.Data.SqlClient.SqlConnection.Open()
仔细用 SQL Server Management Studio 中的 Activity Monitor 查看数据库链接,竟然是只有2,3个数据库链接时,就报上述错误,很是怪异。
一步步删除掉代码,反复试验后,竟然是数据库链接字符串中的 Connect Timeout=0 来作怪的。
比如下述数据库链接字符串就会出现上述问题,
Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=DB1;server=(local);Connect Timeout=0
而把数据库链接字符串修改为
Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=DB1;server=(local)
就不会有问题了。
这个bug在微软的反馈中可以看到,如下:
其中微软的官方反馈是:
The fix was submitted to the source branch of the next major .Net release.
由 Microsoft 在 2008/8/15 11:31 发送
照这么说,估计.net 4.0 中会修复这个bug。
参考资料:
Why Does a Connection Pool Overflow?
http://msdn.microsoft.com/en-us/library/aa175863%28SQL.80%29.aspx
Trace a SqlConnection - Connection Pooling issues
http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Q_22589733.html
Fixing connection pooling timeout exceptions on third-party code
http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=14
Connection Pooling and the "Timeout expired" exception FAQ
http://blogs.msdn.com/angelsb/archive/2004/08/25/220333.aspx
ADO.NET Connection Pooling at a Glance
http://blog.csdn.net/tuwen/archive/2008/05/28/2490299.aspx
http://support.microsoft.com/kb/310617/
ADO.NET数据连接池
http://tech.it168.com/db/s/2006-10-18/200610181013413.shtml
关于ADO.Net连接池(Connection Pool)的一些个人见解
http://www.cnblogs.com/rickie/archive/2004/10/02/48546.aspx
How to: Open Activity Monitor (SQL Server Management Studio)
http://msdn.microsoft.com/en-us/library/ms175518.aspx
Connect Timeout = 0 / Connection Pool Timeout