Paul Speranza's .Net Life

Life with .Net

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!

 

Comments

Ricky Datta said:

yes.
# March 12, 2005 2:28 AM

James said:

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.
# March 12, 2005 3:15 AM

Joe said:

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.
# March 12, 2005 6:33 AM

Feed Search Engine - All Fresh Articles And News Are Here said:

Pingback from  Feed Search Engine - All Fresh Articles And News Are Here

# November 25, 2007 12:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)