CodeGo.net> EntityFramework中的TransactionScope TransactionInDoubtException
作者:互联网
以下代码是我的业务层的一部分:
public void IncrementHits(int ID)
{
using (var context = new MyEntities())
{
using (TransactionScope transaction = new TransactionScope())
{
Models.User userItem = context.User.First(x => x.IDUser == ID);
userItem.Hits++;
try
{
context.SaveChanges();
transaction.Complete();
}
catch (Exception ex)
{
transaction.Dispose();
throw;
}
}
}
}
有时(每周一次或两次),我收到TransactionInDoubtException.堆栈跟踪:
at System.Transactions.TransactionStateInDoubt.EndCommit(InternalTransaction tx)
at System.Transactions.CommittableTransaction.Commit()
at System.Transactions.TransactionScope.InternalDispose()
at System.Transactions.TransactionScope.Dispose()
据我所知,默认隔离级别是可序列化的,因此此原子操作应该没有问题. (假设没有由于写锁定而发生超时)
我该如何解决我的问题?
解决方法:
使用transaction.Rollback而不是transaction.Dispose
如果您的事务处于挂起状态,请始终在发生异常时回滚.
标签:entity-framework-6,transactionscope,asp-net,c,entity-framework 来源: https://codeday.me/bug/20191029/1956490.html