Will the statement used roll back the database transaction when an error occurs?

I have an IDbTransaction in a using statement, but I'm not sure that it will be rolled back if an exception is used in the using statement. I know that the using statement will force the Dispose () call ... but does anyone know if this is true for Rollback ()?

Update: Also, I need to explicitly call Commit (), since I have below or will the usage of the using statement also be taken into account?

My code looks something 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(); } } 
+79
c # using-statement transactions rollback
Mar 13 '09 at 6:32
source share
3 answers

The Dispose method for the transaction class rolls back, but the Oracle class does not. So from a transaction point of view, this is implementation dependent.

The using statement for the connection object, on the other hand, will either close the connection to the database or return the connection to the pool after it is reset. In any case, failed transactions should be rolled back. This is why an exception never leaves an active transaction.

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

+100
Mar 13 '09 at 6:38
source share

You need to call commit. The using statement will do nothing for you.

+19
Mar 14 '09 at 0:57
source share

I believe that if there is an exception such that Commit() has never been called, then the transaction will automatically roll back.

+4
Mar 13 '09 at 6:38
source share



All Articles