I built the following Dispose method for my Unit of Work, which essentially wraps the active NH session and transaction (the transaction is set as a variable after opening the session, so as not to be replaced if the NH session receives a new transaction after an 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 cannot help but feel that the code is a little bloated, will automatic rollback of a transaction be discrete Commit fails in the case of non-nested transactions?
Automatically or automatically disconnect a transaction? If not Session.Close () will automatically delete the associated transaction?
user53791
source share