/// <summary> /// Releases all nhibernate resources allocated to the current thread when /// an exception has occured. /// This method is similar to ReleaseNHSessionFromThread except that it will /// not try to commit the transaction /// </summary> /// <param name="context">the context to use.</param> /// <param name="clearContex">wheather to realse the nhibernate context. /// If a thread is going to be reused leave this option as false /// </param> public static void ReleaseNHSessionFromThreadException(NHContext context, bool clearContex) { log.Debug("Method:" + "ReleaseNHSessionFromThreadException"); NHSessionWrapper wrapper = context.NHSessionWraper; if (wrapper != null && !wrapper.Transaction.WasRolledBack) { try { wrapper.RollbackTransaction(); } catch (HibernateException hEx) { //log.Fatal(SR.UnrecoverableNHibernateCommitException, hEx); throw hEx; } finally { try { if (!wrapper.IsSessionNull) { wrapper.CloseSession(); } } catch { //TODO: log exception throw; } SyncContext.Current.ReleaseMonitored(); //ApplicationContext.Current.Release(); } } if (clearContex) { context.ClearContext(); SyncContext.Current.ClearContext(); //ApplicationContext.Current.ClearContext(); } }
/// <summary> /// Releases all nhibernate resources allocated to the current thread. /// It will also commit any started transactions and close the connection as necesary /// </summary> /// <param name="context">the context to use.</param> /// <param name="clearContex">wheather to realse the nhibernate context. /// If a thread is going to be reused leave this option as false /// </param> public static void ReleaseNHSessionFromThread(NHContext context, bool clearContex) { log.Debug("Method:" + "ReleaseNHSessionFromThread"); NHSessionWrapper wrapper = context.NHSessionWraper; object threadId = CallContext.GetData("threadID"); if (threadId != null) { log.Debug(threadId.ToString()); } else { log.Debug("No thread id found on CallContext"); } log.Debug("Current ThreadID=" + AppDomain.GetCurrentThreadId().ToString()); if (wrapper != null) { try { log.Debug("Try: Checking seesion and transaction for commit"); if (!wrapper.IsSessionNull) { if (!wrapper.Transaction.WasCommitted) { log.Debug("Transaction not commited; about to commit"); wrapper.CommitTransaction(); } } } catch (Exception ex) { //log.Fatal(SR.UnrecoverableNHibernateCommitException, ex); try { log.Debug("Try: about to roll back transaction"); wrapper.RollbackTransaction(); } catch (HibernateException hEx) { //log.Fatal(SR.UnrecoverableNHibernateRollbackException, hEx); throw; } throw; } finally { try { if (!wrapper.IsSessionNull) { log.Debug("Try: about to close session"); wrapper.CloseSession(); } } catch { log.Fatal("Error closing session"); //TODO: Add error message throw; } SyncContext.Current.ReleaseMonitored(); //ApplicationContext.Current.Release(); } } if (clearContex) { log.Debug("Clearing context..."); context.ClearContext(); SyncContext.Current.ClearContext(); //ApplicationContext.Current.ClearContext(); } }