Spring.Data.CannotGetAdoConnectionException:无法获得ADO.NET连接

时间:2016-01-15 22:14:05

标签: c# spring unit-testing nunit

我们使用此命令行在构建服务器上运行了一系列单元测试:

  

mkdir“%WORKSPACE%\ Results”del / Q“%WORKSPACE%\ Results *。*”

     

NCover Run --project =“%JOB_NAME%” - buildId =“%JOB_NAME%

     

%BUILD_NUMBER%V%THE_VERSION%“ - %nunit_exe%输出\ XYZLibTests \ Product.XYZLib.Tests.dll / exclude:效果

     

/ noshadow /xml=Results\XYZLib-TestResults.xml

测试dll是一个c#类库,.NET V4.5,NUnit是NUnit 2.6.4。 我们使用Spring.Net,测试dll的Context.xml文件是:

<objects xmlns="http://www.springframework.net"
         xmlns:db="http://www.springframework.net/database"
         xmlns:tx="http://www.springframework.net/tx">
  <import resource="assembly://Product.Core.SqlServer/Product.Core.Config/Context.xml"/>
  <import resource="assembly://Product.ImportLib/Product.Import.Config/Context.xml"/>
  <db:provider id="dbProvider" provider="SqlServer-2.0"
               connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DBDataFile.mdf;Integrated Security=True"/>
  <object id="transactionManager" type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data">
    <property name="DbProvider" ref="dbProvider"/>
  </object>
  <object name="Logger" type="Spring.Objects.Factory.Config.LogFactoryObject, Spring.Core">
    <property name="logName" value="UnitTestLogger"/>
  </object>
  <object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
    <property name="CommandTimeout" value="240"/>
    <property name="DbProvider" ref="dbProvider"/>
    <property name="DataReaderWrapperType" value="Spring.Data.Support.NullMappingDataReader, Spring.Data"/>
  </object>
</objects>

测试类看起来像这样:

public class ComponentDaoTest
{
        [TestFixtureSetUp]
        public void FixtureSetup()
        {
            this.ComponentDao = Context.GetObject<ComponentDao>();
        }
        [SetUp]
        public void Setup()
        {
            this.ComponentDao.Truncate(); <--cleans localdb attached
        }
//various tests to insert and select from localdb attached
        [TearDown]
        public void Complete()
        {
            this.ComponentDao.Truncate();<--cleans localdb attached
        }
}

ComponentDao正在测试中,它在不同的程序集中定义:

 <object id="ComponentDao" type="Product.Core.SqlServer.ComponentDao, Product.Core.SqlServer">
    <property name="AdoTemplate" ref="adoTemplate"/>
  </object>

它的类定义是:

public class ComponentDao : AdoDaoSupport, IComponentDao

IComponentDao是一个产品特定的接口,它不实现任何已知的接口,如IDisposable。

问题是,这些单元测试运行了一段时间,但我们会看到偶尔我们会得到所有这些的异常(在本例中为FindAllTest):

TearDown Error : Product.CoreLib.Tests.Data.ComponentDaoTest.FindAllTest
16:16:20    SetUp : Spring.Data.CannotGetAdoConnectionException : Could not get ADO.NET connection.
16:16:20   ----> System.Data.SqlClient.SqlException : Connection Timeout Expired.  The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.  This could be because the pre-login handshake failed or the server was unable to respond back in time.  The duration spent while attempting to connect to this server was - [Pre-Login] initialization=15370; handshake=55; 
16:16:20   ----> System.ComponentModel.Win32Exception : The wait operation timed out
16:16:20 TearDown : Spring.Data.CannotGetAdoConnectionException : Could not get ADO.NET connection.
16:16:20   ----> System.Data.SqlClient.SqlException : Connection Timeout Expired.  The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.  This could be because the pre-login handshake failed or the server was unable to respond back in time.  The duration spent while attempting to connect to this server was - [Pre-Login] initialization=15370; handshake=55; 
16:16:20   ----> System.ComponentModel.Win32Exception : The wait operation timed out
16:16:20    at Spring.Data.Support.ConnectionUtils.GetConnectionTxPair(IDbProvider provider)
16:16:20    at Spring.Data.Core.AdoAccessor.GetConnectionTxPair(IDbProvider provider)
16:16:20    at Spring.Data.Core.AdoTemplate.Execute(ICommandCallback action)
16:16:20    at Spring.Data.Core.AdoTemplate.ExecuteNonQuery(CommandType cmdType, String cmdText)
16:16:20    at Product.Core.SqlServer.ComponentDao.Truncate() in c:\Jenkins\Workspace\...\CoreLib\SqlServer\ComponentDao.cs:line 133
16:16:20    at Product.CoreLib.Tests.Data.ComponentDaoTest.Setup() in c:\Jenkins\Workspace\...\CoreLib.Tests\Data\ComponentDaoTest.cs:line 39
16:16:20 --SqlException
16:16:20    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
16:16:20    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
16:16:20    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
16:16:20    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
16:16:20    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
16:16:20    at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
16:16:20    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
16:16:20    at System.Data.SqlClient.SqlConnection.Open()
16:16:20    at Spring.Data.Support.ConnectionUtils.DoGetConnection(IDbProvider provider)
16:16:20    at Spring.Data.Support.ConnectionUtils.GetConnectionTxPair(IDbProvider provider)
16:16:20 --Win32Exception
16:16:20 
16:16:20 --TearDown
16:16:20    at Spring.Data.Support.ConnectionUtils.GetConnectionTxPair(IDbProvider provider)
16:16:20    at Spring.Data.Core.AdoAccessor.GetConnectionTxPair(IDbProvider provider)
16:16:20    at Spring.Data.Core.AdoTemplate.Execute(ICommandCallback action)
16:16:20    at Spring.Data.Core.AdoTemplate.ExecuteNonQuery(CommandType cmdType, String cmdText)
16:16:20    at Product.Core.SqlServer.ComponentDao.Truncate() in c:\Jenkins\Workspace\...\CoreLib\SqlServer\ComponentDao.cs:line 133
16:16:20    at Product.CoreLib.Tests.Data.ComponentDaoTest.Complete() in c:\Jenkins\Workspace\...\CoreLib.Tests\Data\ComponentDaoTest.cs:line 48
16:16:20 --SqlException
16:16:20    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
16:16:20    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
16:16:20    at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
16:16:20    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
16:16:20    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
16:16:20    at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
16:16:20    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
16:16:20    at System.Data.SqlClient.SqlConnection.Open()
16:16:20    at Spring.Data.Support.ConnectionUtils.DoGetConnection(IDbProvider provider)
16:16:20    at Spring.Data.Support.ConnectionUtils.GetConnectionTxPair(IDbProvider provider)
16:16:20 --Win32Exception

我猜所有测试都使用与Context.xml中配置的相同localdb数据库相同的Ado Connection,但问题是,有时它们都运行没有错误。这些测试在很长一段时间内都没有改变,所以开关失败让我们感到困惑。

0 个答案:

没有答案