Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net TransactionScope Exception

My Code something like this

try
{
    using (TransactionScope iScope = new TransactionScope())
    {
        try
        {
            isInsertSuccess = InsertProfile(account);
        }
        catch (Exception ex)
        {
            throw;
        }

        if (isInsertSuccess)
        {
            iScope.Complete();
            retValue = true;
        }
    }
}
catch (TransactionAbortedException tax)
{
    throw;
}
catch (Exception ex)
{
    throw;
}

Now what happen is that even if my value is TRUE a TransactionAbortedException Exception occurs randomly, but data get's inserted/updated in DB.

Any idea what went wrong?

like image 600
Posto Avatar asked Aug 04 '26 04:08

Posto


1 Answers

As the TransactionAbortedException documentation says,

This exception is also thrown when an attempt is made to commit the transaction and the transaction aborts.

This is why you see the exception even after calling Transaction.Complete: the Complete method is not the same thing as Commit:

calling this method [TransactionScope.Complete] does not guarantee a commit of the transaction. It is merely a way of informing the transaction manager of your status

The transaction isn't committed until you exit the using statement: see the CommittableTransaction.Commit documentation for details. At that point any actions participating in the transaction may vote to abort the transaction and you'll get a TransactionAbortedException.

To debug the underlying problem you need to analyze the exception details and stack trace. As Mark noted in a comment, it may well be caused by a deadlock or another interaction with other database processes.

like image 161
Jeff Sternal Avatar answered Aug 06 '26 18:08

Jeff Sternal