views:

52

answers:

2

What are the cases when timeout occurs in SqlConnection.Open()?

On one of our IIS boxes 10 seconds after AppProcess was recycled the following exception appeared:

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& 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()

According to MSDN Number 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.

After 2 seconds similar exception appeared for the same thread, but in the different place. The call stack was much shorter:

    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()

The interval between operation that tries to communicate to DB and the error is less than second.

Questions: 1. How to reproduce such errors? 2. Why call stack is shorter in the second case?

A: 

There can be some reasons such as your SQL server is not able to process your job.. Generally server which is hosting SQL Server, Ram - CPU Process goes up, and your sql cant give a response to your call. increase your timeout duration, and wait more (If you sure, your connection settings are all correct).

Serkan Hekimoglu
Serkan -- the interval between method call and error is less than usual connection timeout (15 seconds). In other case it happened almost immediately.
the_joric
So it seems there isnt a connection problem. I have a question. Your Sql is running on local or on server? It its running on server, check the server's Event Logs. Is there any log about Network or Sql?
Serkan Hekimoglu
SQL server is remote but we have no access to it. I put a request to DBA. Just wondering what may cause the issue.
the_joric
A: 

Some cases of timeout in SqlConnection.Open() refers to a connection leak. It can occur when the connection pool is out of connections. First you should check number of reclaimed connections counter of IIS machine. It should be 0. If not you should check your code to ensure that sql connections are closed properly.

hakan
hakan, in that case there would be different exception type, though the text will be almost the same:System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
the_joric