Connection timeout set to 5 sec but takes longer to timeout

  • Thread starter Thread starter Avi
  • Start date Start date
A

Avi

Hi all,



My Connection string timeout was set to 5 sec but commands takes much longer
to timeout when there is no connection to the destination ip. Is the
timeout value only helps when the ip is reachable but the sql server is not,
or it should work also if the ip address is unreachable. If it should work
also when the ip is unreachable, why it does not work in my case?

Thanks a million,

Avi

public static int ExecCommand(string command, string ip)

{

if (String.IsNullOrEmpty(ip))

throw new ManualRedundancyException("IP address for database connection
cannot be null");

string connectionStr = "Data Source=" + ip + ";Initial
Catalog=master;Persist Security Info=True;User
ID=sa;Password=1210;Connection Timeout=5;";

using (SqlConnection cn = new SqlConnection(connectionStr))

{

using (SqlCommand cm = new SqlCommand(command, cn))

{

cn.Open();

int i = cm.ExecuteNonQuery();

return i;

}

}

}
 
Thanks for the reply.

I'm only talking to the connection timeout. Event when I set the timeout in
the connection string to 5 seconds it will take much longer to get timeout
if the IP address in unreachable.

Avi
 
If I remember a similar thread you have a limit anyway under which you can't
go.

How much time does it take now ? If I remember you can't go between 10/15 s.
If still higher it could be perhaps another problem (name resolution being
slow ?)
 
http://www.connectionstrings.com/sql-server-2005
Connect via an IP address

If you try it with the "most anal" connection string....

where you specify the
IPAddress
Port
Protocol ("Network Library" in the above URL)

(And don't forget to throw in the Connection Timeout as well (as you are
doing in your original sample).
Connection Timeout=30";

Which behavior does it exhibit?


..................

And have you verified the connection string value is being written?
(This is a long shot I realize......but better safe than sorry)


using (SqlConnection cn = new SqlConnection(connectionStr))
{
Console.WriteLine("Just making super sure : " +
Convert.ToString(cn.ConnectionTimeout); // << What are you getting here?
//your other code
}
 
Back
Top