Sql Connection Error 40 Under Load

六月ゝ 毕业季﹏ 提交于 2020-02-07 05:12:53

问题


My database connections from my web servers work fine normally, but when a write heavy windows service I have triggers, I will randomly get "A network-related or instance-specific error occurred while establishing a connection to SQL Server. Error 40." from my webservers.

I have tried turning connection pooling off for my web servers since they share the same connection string as the windows service, but I am at a loss as to what is still causing this.

My SqlConnection objects are all wrapped in using statements as well, so they are being disposed.

Dapper is being used currently for the query extension method.

Connection String example: "Data Source=SERVER;initial catalog=DBNAME;user id=USERNAME;password=PASSWORD;Pooling=False;"

using (IDbConnection connection = new SqlConnection(_connectionString))
{
     return connection.Query<TotalPosts>("analytics.TotalPosts_Sel", new { AccountId = accountId }, commandType: CommandType.StoredProcedure).FirstOrDefault();
}

回答1:


Please follow those steps:

1- Make sure your SQL Server Service is started.

2- Make sure TCP/IP is enabled in SQL Server Configuration Manager, Right-click TCP-IP, Go to Properties and make sure the default port is set to 1433.

3- Open your Windows Firewall. Click "Change Settings" link. When a new popup is shown, click "Add Port" button. Add 1433 as a TCP Port for SQL:

4- Connect to your SQL Connection in SSMS, Right-click and go to Properties. Then, make sure "Allow Remote Connections" checkbox is checked.

Reference: https://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error/




回答2:


OK, I realize this question is several years old, but as I hit the same problem in 2019 with .NET 4.8, I will post the problem, and a workaround.

Your problem is caused because of the speed you create and close connections and the number of connections that remain opened. You are causing a bottleneck in the low level database driver/library.

Some useful information to understand the problem:

https://support.microsoft.com/en-gb/help/328476/description-of-tcp-ip-settings-that-you-may-have-to-adjust-when-sql-se

Using connection pooling can bring other issues as well:

http://florianreischl.blogspot.com/2011/08/adonet-connection-pooling-internals-and.html

My workaround was to sleep the thread for 20 ms. after each iteration. Dirty, but worked.

Regards,

MarianoC.



来源:https://stackoverflow.com/questions/26746358/sql-connection-error-40-under-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!