title: Why timeout may occur in SqlConnection.Open()?
body: <p>What are the cases when timeout occurs in SqlConnection.Open()?</p>
  
  <p>On one of our IIS boxes 10 seconds after AppProcess was recycled the following exception appeared:</p>
  
  <pre><code>Type : System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
      Message : Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
      Source : .Net SqlClient Data Provider
      Help link : 
      Errors : System.Data.SqlClient.SqlErrorCollection
      Class : 11
      LineNumber : 0
      Number : -2
      Procedure : 
      Server : XXX
      State : 0
      ErrorCode : -2146232060
      Data : System.Collections.ListDictionaryInternal
      TargetSite : Void OnError(System.Data.SqlClient.SqlException, Boolean)
      Stack Trace :    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
         at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
         at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
         at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
         at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
         at System.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean&amp; marsCapable)
         at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
         at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
         at System.Data.SqlClient.SqlInternalConnectionTds.LoginWithFailover(Boolean useFailoverHost, String primaryHost, String failoverHost, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
         at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
         at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
         at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
         at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
         at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
         at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
         at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
         at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
         at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
         at System.Data.SqlClient.SqlConnection.Open()
         at NHibernate.Connection.DriverConnectionProvider.GetConnection()
         at NHibernate.Impl.SessionFactoryImpl.OpenConnection()
  </code></pre>
  
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/5h253hsf.aspx" rel="noreferrer">MSDN</a> <strong>Number</strong> property contains SQL Engine Error. But I was not able to find error -2 in master.dbo.sysmessages.
  The interval between operation that tried to open the connection was about 3.5 seconds. The connection timeout has not been explicitly changed.</p>
  
  <p>After 2 seconds similar exception appeared for the same thread, but in the different place. The call stack was much shorter:</p>
  
  <pre><code>    Type : System.Data.SqlClient.SqlException, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  Message : Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
  Source : .Net SqlClient Data Provider
  Help link : 
  Errors : System.Data.SqlClient.SqlErrorCollection
  Class : 11
  LineNumber : 0
  Number : -2
  Procedure : 
  Server : XXX
  State : 0
  ErrorCode : -2146232060
  Data : System.Collections.ListDictionaryInternal
  TargetSite : Void OnError(System.Data.SqlClient.SqlException, Boolean)
  Stack Trace :    at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
     at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
     at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
     at System.Data.SqlClient.SqlConnection.Open()
     at NHibernate.Connection.DriverConnectionProvider.GetConnection()
     at NHibernate.Impl.SessionFactoryImpl.OpenConnection()
  </code></pre>
  
  <p>The interval between operation that tries to communicate to DB and the error is less than second.</p>
  
  <p>Questions:
  1. How to reproduce such errors?
  2. Why call stack is shorter in the second case?</p>