Connection.open为无限期挂起,而不会抛出异常 [英] Connection.open for hangs indefinitely, no exception is thrown

查看:167
本文介绍了Connection.open为无限期挂起,而不会抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试做下面的代码,程序无限期挂起。我不知道为什么,并似乎有对此事的其他悬而未决的话题。虽然,如果IP\website无法达成,那么就按预期工作。



<预类=郎-CS prettyprint-覆盖> 私人无效DoStuff()
{
字符串的connectionString =数据源= www.google.com;连接超时= 5;
使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
connection.Open(); //这里无限期挂起
Console.WriteLine(测试);
}
}

例如,如果我的连接字符串设置为



的connectionString =数据源= www.nonexistentsite.com;连接超时= 5;



那么就会抛出一个异常。我如何得到它抛出一个异常,一个活跃的网站? ...此外谷歌只是出于测试目的,显然



编辑:



如果我尝试连接到服务器无法访问的名称或IP地址我会这种例外...



建立SQL Server的连接时发生网络相关的或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server配置为允许远程连接。 (provider:命名管道提供程序,error:40 - 无法打开到SQL Server的连接)



更新:



让程序运行相当长一段时间,它通常超时最后3-5分钟后,给我我张贴以上错误后。我怎样才能得到它超时更快?


解决方案

如果您设置了FQDN(完全合格域名)为你的数据源 example.com 和DNS服务器无法解析此FQDN很长一段时间它是很明显,您的请求将挂出。请确保您从中运行应用程序的机器可以达到SQL服务器和解决它没有任何问题。您还可能希望确保没有防火墙,可能会阻止该请求。



另一个可能的原因这些症状是,如果你已用尽的ADO连接池。净。这种情况可能发生,如果您有并行运行速度慢许多SQL查询,他们每个人取到数据库的物理连接。有一个在这个游泳池和可连接数的限制,当达到该限制为下一次调用connection.Open()将返回可能会等待一个可用连接游泳池



备注:您可能还需要在连接字符串中指定要如何对SQL服务器进行身份验证。结帐 connectionstrings.com 更多的例子。



这一切是说,是绝对没有错的,你已经张贴在你的问题的C#代码。它看起来更像是,你可以把你的网络管理员注意网络相关的问题。


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?

解决方案

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.

这篇关于Connection.open为无限期挂起,而不会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆