With NHibernate and Transaction do I rollback on commit failure or does it auto rollback on single c

Posted by mattcodes on Stack Overflow See other posts from Stack Overflow or by mattcodes
Published on 2009-09-08T08:26:53Z Indexed on 2010/06/13 3:02 UTC
Read the original article Hit count: 279

Filed under:
|

I've built the following Dispose method for my Unit Of Work which essentially wraps the active NH session & transaction (transaction set as variable after opening session as to not be replaced if NH session gets new transaction after error)

 public void Dispose()
    {
        Func<ITransaction,bool>  transactionStateOkayFunc = 
            trans => trans != null && trans.IsActive && !trans.WasRolledBack;

        try {
            if(transactionStateOkayFunc(this.transaction))
            {
                    if (HasErrored)
                    {
                        transaction.Rollback();
                    }
                    else
                    {
                        try
                        {
                            transaction.Commit();
                        } catch (Exception)
                        {
                            if(transactionStateOkayFunc(transaction)) transaction.Rollback();
                            throw;
                        }
                    }
                }
        } finally
        {
            if(transaction != null) transaction.Dispose();
            if(session.IsOpen) session.Close();
        }

I can't help feeling that code is a little bloated, will a transaction automatically rollback is a discrete Commit fails in the case of non-nested transactions?

Will Commit or Rollback automatically Dipose the transaction? If not will Session.Close() automatically dispose the associated transaction?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about transactions