如何更快地做出的SqlConnection超时 [英] How to make SqlConnection timeout more quickly

查看:451
本文介绍了如何更快地做出的SqlConnection超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用与SqlClient.SqlConnection一个SQL连接字符串和字符串中指定连接超时= 5,但它仍在等待返回故障前30秒。我如何让它放弃,回到快?我是一个快速的本地网络上,并且不想等待30秒。未开启的服务器需要30秒失败。这仅仅是一个快速的实用程序,那将永远只是这本地网络上运行。

I am using an SQL connection string with SqlClient.SqlConnection and specifying Connection Timeout=5 in the string, but it still waits 30 seconds before returning failure. How do I make it give up and return faster? I'm on a fast local network and don't want to wait 30 seconds. The servers that are not turned on take 30 seconds to fail. This is just a quick utility program that's going to always run just on this local network.

修改:对不起,如果我不清楚。我希望在SQLConnection.open更快地失败。希望这可以推断的事实,我想更快地失败,服务器被关闭。

Edit: Sorry if I was unclear. I want the SqlConnection.Open to fail more quickly. Hopefully that could be deduced from the fact that the servers I want to fail more quickly are turned off.

修改:看来这个设置只是有时会失败。就像它知道服务器的IP地址,并使用TCP / IP去跟它(不是本地),但不能在该地址与SQL Server的?我不知道的模式是什么,但是当本地连接与SQL Server停止我看不出这个问题,并试图连接到一个不存在的服务器,当我看不出来。我已经看到了试图联系其中Windows 2008的防火墙阻止SQL Server的服务器,虽然当。

Edit: It seems that the setting only fails sometimes. Like it knows the IP address of the server, and is using TCP/IP to talk to it (not local) but can't contact SQL Server at that address? I'm not sure what the pattern is, but I don't see the problem when connecting locally with SQL Server stopped, and I don't see it when attempting to connect to a non-existent server. I have seen it when attempting to contact a server where the Windows 2008 firewall is blocking SQL Server, though.

推荐答案

看起来一切都造成长期拖延的情况下能够得到解决的的更快地尝试这样的直接套接字连接:

It looks like all the cases that were causing long delays could be resolved much more quickly by attempting a direct socket connection like this:

foreach (string svrName in args)
{
   try
   {
      System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient(svrName, 1433);
      if (tcp.Connected)
         Console.WriteLine("Opened connection to {0}", svrName);
      else
         Console.WriteLine("{0} not connected", svrName);
      tcp.Close();
   }
   catch (Exception ex)
   {
      Console.WriteLine("Error connecting to {0}: {1}", svrName, ex.Message);
   }
}

我要使用这个code,以检查服务器的SQL Server的端口上进行响应,并且只尝试,如果它不打开一个连接。我以为(根据别人的经验),将有30秒的延迟,即使在这个水平,但我得到一个消息,本机,积极拒绝连接这些的时候了。

I'm going to use this code to check if the server responds on the SQL Server port, and only attempt to open a connection if it does. I thought (based on others' experience) that there would be a 30 second delay even at this level, but I get a message that the machine "actively refused the connection" on these right away.

编辑:,然后如果机器不存在,它告诉我,马上过。没有30秒的延迟,我可以找到。

And if the machine doesn't exist, it tells me that right away too. No 30-second delays that I can find.

编辑:机器被在网络上,但不会关闭仍需要30秒失败,我猜。该防火墙机器无法更快,虽然。

Machines that were on the network but are not turned off still take 30 seconds to fail I guess. The firewalled machines fail faster, though.

编辑:下面是更新code。我觉得这是更清洁关闭套接字不是中止线程:

Here's the updated code. I feel like it's cleaner to close a socket than abort a thread:

static void TestConn(string server)
{
   try
   {
      using (System.Net.Sockets.TcpClient tcpSocket = new System.Net.Sockets.TcpClient())
      {
         IAsyncResult async = tcpSocket.BeginConnect(server, 1433, ConnectCallback, null);
         DateTime startTime = DateTime.Now;
         do
         {
            System.Threading.Thread.Sleep(500);
            if (async.IsCompleted) break;
         } while (DateTime.Now.Subtract(startTime).TotalSeconds < 5);
         if (async.IsCompleted)
         {
            tcpSocket.EndConnect(async);
            Console.WriteLine("Connection succeeded");
         }
         tcpSocket.Close();
         if (!async.IsCompleted)
         {
            Console.WriteLine("Server did not respond");
            return;
         }
      }
   }
   catch(System.Net.Sockets.SocketException ex)
   {
      Console.WriteLine(ex.Message);
   }
}

这篇关于如何更快地做出的SqlConnection超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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