TransactionScope TransactionInDoubtException in EntityFramework

asp.net c# entity-framework entity-framework-6 transactionscope

Question

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 a 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 should be no problem with this atomic operation. (Assuming there is no timeout occuring because of a write lock)

How can I fix my problem?

1
3
3/10/2018 3:23:45 PM

Popular Answer

Use transaction.Rollback instead of transaction.Dispose

If you have a transaction in a pending state always rollback on exception.

4
11/9/2014 4:34:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow