BizTalk Blog by Chris Han

System Design for Enterprise Agility,

  Home  |   Contact  |   Syndication    |   Login
  65 Posts | 9 Stories | 122 Comments | 79 Trackbacks

News

Article Categories

Archives

Post Categories

Image Galleries

BizTalk Bloggers

BizTalk on MSDN

Patterns & Architecture

SharePoint

I got this error when testing my web service POC which is runing on .netFx 2.0:

System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.

I find Angel's article gives the most accurate diagnosis. Check it out here:
http://blogs.msdn.com/angelsb/archive/2004/08/25/220333.aspx

But the solution he gives out doesn't solve the problem:
      public void DoesNotLeakConnections()

      {          

                  Using (SqlConnection sqlconnection1 = new SqlConnection("Server=.\\SQLEXPRESS ;Integrated security=sspi;connection timeout=5")) {

                              sqlconnection1.Open();

                              SqlCommand sqlcommand1 = sqlconnection1.CreateCommand();

                              sqlcommand1.CommandText = "raiserror ('This is a fake exception', 17,1)";

                              sqlcommand1.ExecuteNonQuery();  //this throws a SqlException every time it is called.

                              sqlconnection1.Close(); //Still never gets called.

                  } // Here sqlconnection1.Dispose is _guaranteed_

      }


What you should do which is the best practice for any resource related job is to close the resource in a Final block like this:

 

      public void DoesNotLeakConnections()  

      {          

                  Using (SqlConnection sqlconnection1 = new SqlConnection("Server=.\\SQLEXPRESS ;Integrated security=sspi;connection timeout=5")) {

try

{

                              sqlconnection1.Open();

                              SqlCommand sqlcommand1 = sqlconnection1.CreateCommand();

                              sqlcommand1.CommandText = "raiserror ('This is a fake exception', 17,1)";

                              sqlcommand1.ExecuteNonQuery();  //this throws a SqlException every time it is called.

}

catch  (Exception ex)

{

  //do something

}

finally

 

 

{

          sqlconnection1.Close(); //Always gets called.

}

                  } // Here sqlconnection1.Dispose is _guaranteed_

      }


 

posted on Thursday, June 28, 2007 9:49 AM

Feedback

# re: Error: System.Data.SqlClient.SqlException: Timeout expired 6/28/2007 11:16 AM Paul Wilson
But the implementation of Dispose DOES call Close, which is the recommended pattern for IDisposable implementations. So adding the extra try/finally does not accomplish anymore in reality, except that you've caught the exception.

# re: Error: System.Data.SqlClient.SqlException: Timeout expired 7/5/2007 4:39 PM chris
errrr, it turned out is because SqlCommand.CommandTimeout Property by default 30s. I only set connecition time out.
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx


# re: Error: System.Data.SqlClient.SqlException: Timeout expired 1/14/2009 9:15 PM william navarrete
I need to continue my work urgently.Please,I need help.

# re: Error: System.Data.SqlClient.SqlException: Timeout expired 1/15/2009 9:07 AM chris
what's the problem, william?

# re: Error: System.Data.SqlClient.SqlException: Timeout expired 3/15/2009 2:48 AM shubham saxena
i am facing the same problem...please give me an idea to resolve it..

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: