ASP.NET 中 TransactionScope 的问题
我构建了一个类来同步两个不同数据源之间的数据。这种同步分为多个部分(和方法)。每个方法都有自己的 TransactionScope,并且方法按顺序运行。
每次运行此代码时,我都会收到以下错误消息:
“与当前连接关联的事务已完成但尚未释放。必须先释放事务,然后才能使用连接来执行 SQL 语句。”
以下代码是使用 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()" 不足以杀死 transactionscope.. 有谁知道什么我做错了吗?
提前致谢!
更新 感谢您的回复。经过几次测试,我发现这个问题仅在一种方法中有多个查询时才存在。例如:
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();
}
}
I've build a class to synchronize data between two different datasources. This synchronization is divided into multiple parts (and methods). Every method has his own TransactionScope and the methods are run sequentially.
Everytime I Run this code I get the following errormessage:
"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."
The following code is an example of such a method with a 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;
}
}
It seems that the call "_transactionScope.Complete()" isn't enough to kill the transactionscope.. Does anyone have a clue what i'm doing wrong?
Thanks in advance!
UPDATE
Thanks for your replies. After a few tests I discovered that this problem only exists when there are multiple queries in one method. for example:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更改构造函数。
Try changing the constructor.
我做了一种在事务范围上创建最大超时值的方法
,然后您将使用它:
I did a method for creating a Max Timeout value on a transaction scope
and then you would use it: