views:

76

answers:

1

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 an TransactionAbortedException Exception is occurs randomly , but data get's inserted/updated in DB

Any idea What went wrong.

+5  A: 

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.

Jeff Sternal
+1 Good explanation, particularly pointing out that Complete is just a vote.
Mark Seemann