Connection.open for hangs indefinitely, no exception is thrown

萝らか妹 提交于 2019-12-30 06:01:08

问题


When I try to do the following code, the program hangs indefinitely. I don't know why and there seems to be other unanswered topics on the matter. Although, if the IP\website cannot be reached, then it works as intended.

    private void DoStuff()
    {
        string connectionString = "Data Source=www.google.com;Connection Timeout=5";
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open(); //Hangs here indefinitely
            Console.WriteLine("Test");
        }
    }

For example, if I set the connection string to

connectionString = "Data Source=www.nonexistentsite.com;Connection Timeout=5";

then it will throw an exception. How do I get it to throw an exception for an active site? ... Also google is just for testing purposes, obviously.

EDIT :

If I try to connect to an unreachable server name or IP address I WILL get this exception...

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

UPDATE :

After letting the program run for quite a while, it usually times out finally after 3-5 minutes and gives me the error I posted above. How can I get it to timeout quicker?


回答1:


If you have set an FQDN (Fully Qualified Domain Name) for your Data Source such as example.com and the DNS server is unable to resolve this FQDN for a long time it is pretty obvious that your request will hang out. Make sure that the machine from which you are running your application can reach the SQL server and resolve it without any issues. Also you probably want to make sure that there is no firewall that might be blocking the request.

Another possible cause for those symptoms is if you have exhausted the connection pool of ADO.NET. This could happen if you have many slow SQL queries running in parallel, each of them taking a physical connection to the database. There is a limit in the number of available connections on this pool and when this limit is reached the next call to connection.Open() might wait for an available connection to be returned to the pool.

Remark: you might also need to specify in your connection string how you want to authenticate against the SQL server. Checkout connectionstrings.com for more examples.

All this is to say that there is absolutely nothing wrong in the C# code you have posted in your question. It looks more like a network related problem that you could bring to the attention of your network administrators.




回答2:


To get the connection to exit after a specified amount of time without success, you can use the Connection Timeout parameter in the connection string. The number you specify is in seconds, so for example, Connection Timeout=240 is equal to 240 seconds\60 seconds = 4 minutes.

Sample connection string:

<add name="MyConnectionString"
connectionString="
Data Source=MyServer\MSSQL2017;
Initial Catalog=MyDatabase;
Integrated Security=True;
Connection Timeout=10;"/>

In the above connection string, the Open() command will timeout after 10 seconds.



来源:https://stackoverflow.com/questions/16448857/connection-open-for-hangs-indefinitely-no-exception-is-thrown

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