RTOHelper.RTOScope.Transaction.Begin();
try
{
//Change Things
RTOHelper.RTOScope.Transaction.Commit();
}
catch
(Exception ex){
RTOHelper.RTOScope.Transaction.Rollback();
}
This is what I've been doing, but when an exception hits it tells me no Active Transaction... (I can do the check for IsActive...but is that how it's done? When will it ever be active?
4 Answers, 1 is accepted
You are on the right path. Do the check for IsActive on the transaction before doing the Rollback in your catch block. I do this all the time in my code.
I can't remember the cases where you will end up with an inactive transaction.
Regards
Henrik
Whenever you call scope.Transaction.Begin() you start an active transaction. The other way to start a transaction is to start it implicitly when the scope.TransactionProperties.AutomaticBegin is set to true. It is active until the scope.Transaction.Commit() is called. When an exception occurs before the Commit method is invoked you still have the Transaction active and you can rollback it safely in the catch block. In case the exception occurs during the commit operation we internally rollback the transaction – so when the catch block code is executed the transaction is inactive. Therefore, it is a good practice to check the status of the transaction before rolling it back.
I hope this information will wipe the confusion. If you want to understand the reason for the exception in your case, please provide the full code.
Best wishes,Petko_I
the Telerik team
That's actually where my question arose from funnily enough...the exception was happening BEFORE the commit, and then dumping into the catch and I get an exception...I figured it wasn't a big deal because I CAN check for Active, and if I haven't committed anything there wasn't anything to rollback...?
Steve
Closing the transaction when an exception occurs before the commit operation is something that should not happen in general. We would like to further investigate the cause of your problem. Can you provide the code that generates the exception and sends you to the catch block? Regardless of the reason for the error you receive, checking whether the transaction is active is always recommended.
Thanks in advance for your time. I hope to hear from you soon.
All the best,
Petko_I
the Telerik team