How to make SqlConnection timeout more quickly

后端 未结 3 1718
执念已碎
执念已碎 2020-12-09 04:41

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.

3条回答
  •  悲&欢浪女
    2020-12-09 05:12

    Update 2 I suggest rolling your own timeout. Something like this:

    internal static class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine(SqlServerIsRunning("Server=foobar; Database=tempdb; Integrated Security=true", 5));
            Console.WriteLine(SqlServerIsRunning("Server=localhost; Database=tempdb; Integrated Security=true", 5));
        }
    
        private static bool SqlServerIsRunning(string baseConnectionString, int timeoutInSeconds)
        {
            bool result;
    
            using (SqlConnection sqlConnection = new SqlConnection(baseConnectionString + ";Connection Timeout=" + timeoutInSeconds))
            {
                Thread thread = new Thread(TryOpen);
                ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                thread.Start(new Tuple(sqlConnection, manualResetEvent));
                result = manualResetEvent.WaitOne(timeoutInSeconds*1000);
    
                if (!result)
                {
                    thread.Abort();
                }
    
                sqlConnection.Close();
            }
    
            return result;
        }
    
        private static void TryOpen(object input)
        {
            Tuple parameters = (Tuple)input;
    
            try
            {
                parameters.Item1.Open();
                parameters.Item1.Close();
                parameters.Item2.Set();
            }
            catch
            {
                // Eat any exception, we're not interested in it
            }
        }
    }
    

    Update 1

    I've just tested this on my own computer using this code:

    internal static class Program
    {
        private static void Main(string[] args)
        {
            SqlConnection con = new SqlConnection("Server=localhost; Database=tempdb; Integrated Security=true;Connection Timeout=5");
            Console.WriteLine("Attempting to open connection with {0} second timeout, starting at {1}.", con.ConnectionTimeout, DateTime.Now.ToLongTimeString());
    
            try
            {
                con.Open();
                Console.WriteLine("Successfully opened connection at {0}.", DateTime.Now.ToLongTimeString());
            }
            catch (SqlException)
            {
                Console.WriteLine("SqlException raised at {0}.", DateTime.Now.ToLongTimeString());
            }
        }
    }
    

    and it obeys the Connection Timeout value in the connection string. This was with .NET 4 against SQL Server 2008 R2. Admittedly, it's a localhost connection which may give different results but it means I can't replicate the problem.

    I can only suggest trying a similar chunk of code in your network environment and seeing if you continue to see long timeouts.

    Old (incorrect) answer I incorrectly thought the ConnectionTimeout property was settable, but it isn't.

    Try setting SqlConnection.ConnectionTimeout instead of using the connection string.

提交回复
热议问题