TransactionScope TransactionInDoubtException in EntityFramework
The following code is part of my business layer:
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;
}
}
}
}
Sometimes (once or twice a week) I get TransactionInDoubtException
. Stacktrace:
at System.Transactions.TransactionStateInDoubt.EndCommit(InternalTransaction tx)
at System.Transactions.CommittableTransaction.Commit()
at System.Transactions.TransactionScope.InternalDispose()
at System.Transactions.TransactionScope.Dispose()
As far as I know, the default isolation level is serializable, so there shouldn't be any problem with this atomic operation. (Assuming no timeout due to write lock)
How can I fix my problem?
source to share
msdn says, "If a transaction manager loses contact with a subordinate after sending a one-phase commit request, but before receiving notification of the results, it has no reliable mechanism to recover the actual result of the transaction. Therefore, the transaction manager sends the result" Doubt "to any claims or voters pending notification of information outcome "
Please check out these links
source to share
Will it behave correctly if you moved the try / catch to include a use directive for the transaction scope? The using directive should implicitly call Dispose for you (so this is clearly overkill) and then you can throw an exception. This is what I see in examples from MSDN:
https://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.dispose(v=vs.110).aspx
source to share