如何在第一次测试时保持SQL Server Express连接超时?

时间:2009-04-17 14:48:10

标签: sql-server sql-server-express

我们对数据访问层(C#)和数据库(MS SQL)之间的交互进行了一些自动化测试。我们使用SQL Express来安装mdf,我们在测试完成后还原它。似乎第一次在新启动的机器上运行测试,即使SQLExpress正在运行,我们也会看到超时。第二次,他们运行得很好。

查询字符串示例:

Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\TEST_DATA.mdf; Integrated Security=True; User Instance=True

示例错误:

   [nunit2] 1) Test : System.Data.SqlClient.SqlException : Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
   [nunit2]    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   [nunit2]    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   [nunit2]    at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
   [nunit2]    at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
   [nunit2]    at System.Data.SqlClient.TdsParserStateObject.ReadPacket(Int32 bytesExpected)
   [nunit2]    at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
   [nunit2]    at System.Data.SqlClient.TdsParserStateObject.ReadByte()
   [nunit2]    at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   [nunit2]    at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
   [nunit2]    at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
   [nunit2]    at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
   [nunit2]    at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   [nunit2]    at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   [nunit2]    at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   [nunit2]    at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   [nunit2]    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   [nunit2]    at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   [nunit2]    at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   [nunit2]    at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   [nunit2]    at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   [nunit2]    at System.Data.SqlClient.SqlConnection.Open()
   [nunit2]    at Test()

有没有人经历过这个?你找到了解决方法吗?

2 个答案:

答案 0 :(得分:1)

当你第一次碰到它或者设置了自动关闭时,以及当你第一次必须由引擎打开时,它仍在恢复。通过执行以下命令检查此设置,将msdb更改为您的数据库名称,如果返回1,则将其设置为autoclose

SELECT DATABASEPROPERTYEX( 'msdb' , 'IsAutoClose' )

答案 1 :(得分:0)

看起来这个问题除了增加连接超时或只是重试之外没有太多解决方案。这只是服务器在新系统上启动数据库的成本。在我的测试中,它在重新启动后发生,但停止和启动SQL服务不会导致问题。 This thread on EE提出了类似的问题。