Transactions and the using statement

I was wondering if someone could clarify this for me. If I have the following code (simplified for brevity) :

using (OracleConnection conn = new OracleConnection() )

{

    using (OracleTransaction trans = conn.BeginTransaction() )

    {

           // Some working code...

 

           trans.Commit();

    }

}


If an exception occurs and commit is never called are all changes absolutely discarded when the transaction is disposed or is the database left in some wierd state?

Thanks in advance!

 

2 Comments

  • Using is syntax suger for a try/finally block with a call to Dispose in the finally.



    It depends what happens in the OracleTransaction Dispose method if commit was never called. I imagine it would rollback the transaction but have a look in Reflector to make sure.

  • I always use this pattern for brevity, having verified a long time ago with Reflector that the Transaction class for all Microsoft data providers (Sql, Odbc, OleDb, Oracle) have a Dispose method which rolls back the transaction if it isn't committed.



    As James said, Reflector is your friend for cases like this, though it would be better if it were explicitly stated in the MSDN documentation.

Comments have been disabled for this content.