.net TransactionScope Exception

.net, c#, transactions

Solution

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.

Problem

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?

Original source