Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will a using statement rollback a database transaction if an error occurs?

I've got an IDbTransaction in a using statement but I'm unsure if it will be rolled back if an exception is thrown in a using statement. I know that a using statement will enforce the calling of Dispose()...but does anyone know if the same is true for Rollback()?

Update: Also, do I need to call Commit() explicitly as I have below or will that also be taken care of by the using statement?

My code looks sort of like this:

using Microsoft.Practices.EnterpriseLibrary.Data;

...

using(IDbConnection connection = DatabaseInstance.CreateConnection())
{
    connection.Open();

    using(IDbTransaction transaction = connection.BeginTransaction())
    {
       //Attempt to do stuff in the database
       //potentially throw an exception
       transaction.Commit();
    }
}
like image 481
mezoid Avatar asked Mar 13 '09 06:03

mezoid


People also ask

When can a rollback occur in a transaction?

Rolls back an explicit or implicit transaction to the beginning of the transaction, or to a savepoint inside the transaction. You can use ROLLBACK TRANSACTION to erase all data modifications made from the start of the transaction or to a savepoint. It also frees resources held by the transaction.

Which statement is rollback in a database?

The ROLLBACK statement is the inverse of the COMMIT statement. It undoes some or all database changes made during the current transaction. For more information, see "Overview of Transaction Processing in PL/SQL". The SQL ROLLBACK statement can be embedded as static SQL in PL/SQL.

What is a rollback of a database transaction?

A rollback is the operation of restoring a database to a previous state by canceling a specific transaction or transaction set. Rollbacks are either performed automatically by database systems or manually by users.

Can we rollback a transaction after it has committed?

You cannot roll back a transaction once it has commited. You will need to restore the data from backups, or use point-in-time recovery, which must have been set up before the accident happened.


3 Answers

Dispose method for transaction class performs a rollback while Oracle's class doesn't. So from transaction's perspective it's implementation dependent.

The using statement for the connection object on the other hand would either close the connection to the database or return the connection to the pool after resetting it. In either case, the outstanding transactions should be rolled back. That's why an exception never leaves an active transaction lying around.

Also, yes, you should call Commit() explicitly.

like image 168
Sedat Kapanoglu Avatar answered Oct 12 '22 22:10

Sedat Kapanoglu


You have to call commit. The using statement will not commit anything for you.

like image 31
jhale Avatar answered Oct 12 '22 22:10

jhale


I believe that if there's an exception such that Commit() was never called, then the transaction will automatically rollback.

like image 6
Tommy Hui Avatar answered Oct 12 '22 20:10

Tommy Hui