linq-to-sql – SQL Server“网络相关或特定于实例的错误”每天一次(困惑!)

我们遇到的错误与
this StackOverflow Q相同……

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
   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()
   at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
   at System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe()
   at System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode()
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)

…除了在引用的StackOverflow Q中,一旦发生错误,他们需要重新启动SQL Server – 而我们不会.我们每天会收到一次这个错误,或者每隔几天收到一次错误 – 错误发生后一切都很好,直到下一次发生.

这让我们认为这不是“忘记关闭联系”的问题.我们有一个中等繁忙的ASP.NET 4.0 WebForms / SQL Server 2008 R2应用程序;但我们非常积极,我们没有超过数据库连接的最大数量.

有关这个问题的任何想法,或诊断方法?

最佳答案 以为我会评论我们的进展.

虽然没有一个SQL Server文档/文章/博客提到这个错误可能是由服务器忙碌造成的,但我找到了一个forum posting,其中一些经验丰富的IT专业人员Matt Neerincx说它可以如下:

Possible reasons for this error include:

1. Poor network link from client to server.

2. Server is very busy (meaning high CPU) and cannot respond to new connection attempts.

3. Server is running out of memory (so high memory usage for SQL).

4. tcp-ip layer on client is over-saturated with connection attempts so tcp-ip layer rejects the connection.

5. tcp-ip layer on server side is over-staturated with connection attempts and so tcp-ip layer is rejecting new connections.

6. With SQL 2005 SP2 and later there could be a custom login trigger that rejects your connection.

You can increase the connect timeout to potentially alleviate issues #2, #3, #4, #5.  Setting a longer connect timeout means the driver will try longer to connect and may eventually succeed.

To determine the root cause of these intermittent failures is not super easy to do unfortunately.  What I normally do is start by examining the server environment, is the server constantly running in high CPU for example, this points to #2.  Is the server using a hugh amount of memory, this points to #3.   You can run SQL Profiler to monitor logins and look for patterns of logins, perhaps every morning at 9AM there is a flurry of connections etc...

所以我们现在正沿着这条道路前进 – 减少了在我们的一些批处理查询中同时执行的查询数,优化了一些查询等.

此外,在我们的应用程序连接字符串中,我们增加了连接超时,并将最小池大小设置为20(认为尝试确保应用程序要抓取的一些现有的,未使用的连接,而不是需要建立新连接).

这时,差不多48小时没有收到错误;让我们充满希望.

点赞