views:

28

answers:

2

Looking into the documentation it says that the Rollback method can throw when the transaction is not in pending state (after begin transaction and before commit transaction).

I can't seem to find a way to check whether the transaction can be rollback or not. There isn't a state property either.

A: 

Are you using a TransactionScope object in your c# code. If so, you never need to issue a rollback. If you don't explicitly commit the transaction, it will rollback once the transaction scope object is out of scope.

Randy Minder
yeah, we are using the transactionscope. We are using the DbTransaction object.
pdiddy
+1  A: 

Ideally, just wrap your transaction with a "using" statement or use a TransactionScope object, which will automatically rollback if an exception is thrown or if the transaction is not commited before it goes out of scope.

The rest of the time, as ugly as it is, I usually wrap my rollbacks in an empty try/catch block, because it is almost always in a catch handler that has a more meaningful exception. The idea is that we only want to rollback if we can, but we don't want to start throwing new exceptions if we can't (for any number unpredictable reasons), because the transaction will get rolled back as long as it's not committed anyway. You still need to try to clean up properly so it doesn't need to wait for the garbage collector, but if you can't, then the rollback isn't the real problem.

try
{
  SqlTransaction trans = connection.BeginTransaction();
  ///blah blah blah
}
catch(Exception theExceptionICareAbout)
{
  try
  {
    if(trans != null)
    {
      trans.Rollback();
    }
  }
  catch {}
  throw;  //re-throws the meaningful exception.
}

Note: don't explicity re-throw the exception (i.e. "throw theExceptionICareAbout"), because that will recreate the stack trace. Instead, just use "throw" which will continue the existing exception stack.

Mike Mooney
thanks, that was my last strategy. I was looking for something cleaner, but i guess there's nothing in the api.
pdiddy
Yeah, there very well may be a combination of checks that you can perform that will tell you if a transaction can be rolled back, but at the end of the day it could fail no matter what, so the really important part is to make sure that it doesn't corrupt your exception stack. Normally I don't like rely on exceptions, but there's not much you can do in this case, and you are almost always in an exceptional case by this point anyway.
Mike Mooney