我已经建立了一个类来同步两个不同数据源之间的数据。这个同步分为多个部分(和方法)。每个方法都有自己的TransactionScope,这些方法按顺序运行。
每次我运行这段代码时,我都会收到以下错误消息:
"The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements."
下面的代码是一个带有TransactionScope的方法示例:
每次我运行这段代码时,我都会收到以下错误消息:
"The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements."
下面的代码是一个带有TransactionScope的方法示例:
private void SomeMethod()
{
try
{
using (var _transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
using (SqlConnection _connection = new SqlConnection(connectionstring))
{
_connection.Open();
DoSomething()...
}
_transactionScope.Complete();
}
}
catch (TransactionAbortedException e)
{
nlog.Error(string.Format("The transaction has been aborted: {0}", e.Message));
throw e;
}
catch (Exception e)
{
throw e;
}
}
看起来调用"_transactionScope.Complete()"并不能足以终止事务范围.. 有没有人知道我做错了什么?
提前感谢!
更新 谢谢你们的回复。经过几次测试,我发现只有在一个方法中有多个查询时才会出现这个问题。例如:
try
{
using (TransactionScope _transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
using (SqlConnection _connection = new SqlConnection(connectionstring))
{
_connection.Open();
//new method:
using (TransactionScope _transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
{
//a selectquery
}
//an update or insert query
_transactionScope.Complete();
}
}