Transaction.Dispose()

Posts   
 
    
wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 22-Jun-2006 11:57:25   

Hi,

I can't find in the documentation if a Transaction.Dispose performs a RollBack() if it hasn't been commited yet.

What i would like to know, instead of this:


using(Transaction transaction = new Transaction(IsolationLevel.Serializable, "Update")){
  try 
  {
      // perform actions
  } catch (Exception) {
     transaction.Rollback();    
     throw;
  }
  transaction.Commit();
}

can I use this syntax?:


using(Transaction transaction = new Transaction(IsolationLevel.Serializable, "Update")){
  // perform actions
  transaction.Commit();
}

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Jun-2006 16:47:11   

I guess when the physical transaction is disposed, all un-comitted tranasactions gets rolled-back.

wtijsma
User
Posts: 252
Joined: 18-Apr-2006
# Posted on: 29-Jun-2006 11:03:23   

Walaa wrote:

I guess when the physical transaction is disposed, all un-comitted tranasactions gets rolled-back.

Thanks.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 20-Jul-2006 00:54:12   

Can this be verified for sure?

I looked at the code and dispose() does not do a rollback, but it does do a dispose() on the physicalConnection property, which should close the connection and roll it back?

Confirmation - as the other code is much cleaner...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 20-Jul-2006 10:01:16   

protected virtual void Dispose(bool isDisposing)
{
    // Check to see if Dispose has already been called.
    if(!_isDisposed)
    {
        if(isDisposing)
        {
            // Dispose managed resources.
            if(_physicalTransaction != null)
            {
#if !CF
                _physicalTransaction.Dispose();
#endif
                _physicalTransaction = null;
            }
            if(_connectionToUse != null)
            {
                // closing the connection will abort (rollback) any pending transactions
                if(_connectionToUse.State == ConnectionState.Open)
                {
                    _connectionToUse.Close();
                }
#if !CF
                _connectionToUse.Dispose();
#endif
                _connectionToUse = null;
            }

            if( this.InSystemTransaction )
            {
#if !CF
                // set transaction and resource manager to null, so the adapter goes out of scope as soon as the System.Transactions.Transaction is going out of scope
                _systemTransactionResourceManager = null;
                _systemTransaction = null;
                _systemTransactionEnlistment = null;
#endif
            }
            _isDisposed = true;
        }
    }
}

i.o.w. it does a rollback.

Frans Bouma | Lead developer LLBLGen Pro