sql-server - 多线程 Windows 服务中多个同时 SQL 连接超时

标签 sql-server multithreading connection-timeout

我有一个用 VS 2010 (.NET 4.0) 开发的多线程 Windows 服务,它可以有几个到几十个线程,每个线程通过 Internet 从慢速服务器检索数据,然后使用本地数据库记录此数据(因此该过程受 Internet 限制,而不是 LAN 或 CPU 限制)。

有规律地,我同时从多个线程中收到大量/困惑/突发的以下错误:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

此错误的调​​用堆栈通常是:

at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)

at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)

at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)

at System.Data.SqlClient.SqlConnection.Open()

我没有在连接字符串中指定连接超时,并且还有其他应用程序和进程正在该数据库中工作。有人遇到过这种行为吗?如果有,采取了什么措施来防止这种行为?

我的数据访问层中最常调用的方法如下所示,并且我所有其他 DAL 方法都遵循相同的方法:

using (SqlConnection con = new SqlConnection(GetConnectionString()))
using (SqlCommand cmd = new SqlCommand("AddGdsMonitorLogEntry", con))
{
    cmd.CommandType = CommandType.StoredProcedure;

    /* setting cmd.Parameters [snipped] */

    // We have been getting some timeouts writing to the log; wait a little longer than the default.
    cmd.CommandTimeout *= 4;

    con.Open();

    cmd.ExecuteNonQuery();
}

非常感谢!

编辑

鉴于有关镜像环境中发生的这种情况的评论,我确实应该提到有问题的数据库是镜像的。它在 SSMS 中标记为“主体,同步”,处于“高安全性,无需自动故障转移(同步)”模式。

编辑 2011 年 5 月 26 日

我在 SQL Server 日志中没有看到任何表明存在任何问题的内容。 (我无权访问该服务器上的 Windows 事件查看器,但我已要求有人来找我。)

最佳答案

根据MSDN Blog post今天刚刚创建(Google 万岁!):

Microsoft has confirmed that this is a problem in the current release of ADO.NET. This issue will be fixed in ADO.NET version, ships with Visual Studio 2011.

In the meantime, we request to use the following workarounds:

  1. Increase the connection string timeout to 150 sec. This will give the first attempt enough time to connect( 150* .08=12 sec)

  2. Add MinPool Size=20 in the connection string. This will always maintain a minimum of 20 connections in the pool and there will be less chances of creating new connection, thus reducing the chance of this error.

  3. Improve the network performance. Update your NIC drivers to the latest firmware version. We have seen network latency when your NIC card is not compatible with certain Scalable Networking Pack settings. If you are on Windows Vista SP1 or above you may also consider disabling Receive Window Auto-Tuning. If you have NIC teaming enabled, disabling it would be a good option.

这篇文章本身很有趣,讨论了 TCP/IP 连接重试算法。感谢所有说“嘿,这看起来与镜像有关......”的人!请注意对此的评论是“由于 SQL Server 响应缓慢或由于网络延迟”。

呃!!!

感谢所有发帖的人。现在我们都必须请求 .NET Framework 的补丁(或其他一些 ADO.NET 补丁机制),这样我们就不必等待(并购买)Visual Studio 11...

关于sql-server - 多线程 Windows 服务中多个同时 SQL 连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5884876/

相关文章:

sql-server - 报表数据库的最佳解决方案

java - Java8 Hibernate5.2 的日期问题

c# - 使用 DateTime.Now.Ticks 生成唯一的数字 ID

sql-server - dockerize TeamCity时找不到MSSQL驱动程序

用于复杂逻辑的 SQL 查询 (pl/sql)

java - 如何确保一个方法只被多个线程调用一次?

java - 在线更新客户名单

asp.net - SQL 连接强行关闭

java - 如何使用 Apache httpclient 4.* 和 PoolingHttpClientConnectionManager 设置每个请求的超时?

c++ - Modbus 通讯减少超时等待时间