views:

256

answers:

1

I am dealing with an interesting situation where I perform many database updates in a single transaction. If these updates fail for any reason, the transaction is rolled-back.

IDbTransaction transaction
try {
    transaction = connection.BeginTransaction();

    // do lots of updates (where at least one fails)

    transaction.Commit();
} catch {
    transaction.Rollback(); // results in a timeout exception
} finally {
    connection.Dispose();
}

I believe the above code is generally considered the standard template for performing database updates within a transaction.

The issue I am facing is that whilst transaction.Rollback() is being issued to SQL Server, it is also timing out on the client.

Is there anyway of distinguishing between a timeout to issue the rollback command and a timeout on that command executing to completion?

Thanks in advance, Ben

A: 

You should specify particular catches i.e. Look at the different exceptions that are thrown. If there is an error whilst processing the sql command then a SqlException should be thrown so catch the different exceptions to differentiate in your project.

Further more you could also catch and program for

Exception - An error occurred while trying to commit the transaction.

InvalidOperationException - The transaction has already been committed or rolled back. -or- The connection is broken.

Andrew

REA_ANDREW
Sure I can catch the undocumented timeout exception (and the others) of the transaction.Rollback() method call, but how exactly will I know whether the rollback occurred or not. I imagine the timeout exception will be raised in 2 different scenarios; the first being if the rollback command could not be issued to SQL Server and the second if the rollback command was issued, but took too long...
I have seen a further implementation of your above example where there is a further try catch around the rollback method call, so here is where you would then catch exceptions thrown by the rollback procedure itself. Again this could be an sql exception if it could not be rolled back, an Exception I would think for a timeout or an Invalid Operation for specific transaction related errors.
REA_ANDREW