Search code examples
.netsql-serversqlclientconnection-timeout

SQL Connection Not Timing Out Quickly When Network Cable Unplugged


First I unplug my network cable.

Then I try to connect to SQL Server from within C# using an SqlClient.SqlConnection object and a connection string that either does not specify a connection timeout (and thus defaults to 15 seconds): Integrated Security=SSPI;Persist Security Info=False;Data Source=MYSERVER;Database=MYDB"

or a connection string that does specify a timeout, even shorter at 5 seconds: "Integrated Security=SSPI;Persist Security Info=False;Data Source=MYSERVER;Database=MYDB;Connect Timeout=5"

Either way, the call to connection.Open returns after 42 seconds with my timeout error. If I plug my network cable back in earlier, I can provoke an error sooner. Why is the connection timeout having no effect? Is this documented somewhere? Is there a way around it?


Solution

  • Connect Timeout applies ONLY when it is able to resolve the server and waits for a connection.

    As per MSDN also, Connect Timeout is the length of time (in seconds - default 15) to wait for a connection to the server before terminating the attempt and generating an error.

    Note:
    5 seconds is too less for networking hardwares to detect the network is available or not. It should not be less than 10 seconds.