CloseSessionOrRegisterDeferredClose() static private method

Close the given Session or register it for deferred close.
static private CloseSessionOrRegisterDeferredClose ( ISession session, ISessionFactory sessionFactory ) : void
session ISession The session.
sessionFactory ISessionFactory The session factory.
return void
Ejemplo n.º 1
0
        /// <summary>
        /// Invoked after transaction commit/rollback.
        /// </summary>
        /// <param name="status">
        /// Status according to <see cref="Spring.Transaction.Support.TransactionSynchronizationStatus"/>
        /// </param>
        /// <remarks>
        /// Can e.g. perform resource cleanup, in this case after transaction completion.
        /// <p>
        /// Note that exceptions will get propagated to the commit or rollback
        /// caller, although they will not influence the outcome of the transaction.
        /// </p>
        /// </remarks>
        public override void AfterCompletion(TransactionSynchronizationStatus status)
        {
            if (!newSession)
            {
                ISession session = sessionHolder.Session;

                // Provide correct transaction status for releasing the Session's cache locks,
                // if possible. Else, closing will release all cache locks assuming a rollback.
                ISessionImplementor sessionImplementor = session as ISessionImplementor;
                if (sessionImplementor != null)
                {
                    sessionImplementor.AfterTransactionCompletion(status == TransactionSynchronizationStatus.Committed, sessionHolder.Transaction);
                }

                if (newSession)
                {
                    SessionFactoryUtils.CloseSessionOrRegisterDeferredClose(session, sessionFactory);
                }
            }
            if (!newSession && status != TransactionSynchronizationStatus.Committed)
            {
                // Clear all pending inserts/updates/deletes in the Session.
                // Necessary for pre-bound Sessions, to avoid inconsistent state.
                sessionHolder.Session.Clear();
            }

            if (this.sessionHolder.DoesNotHoldNonDefaultSession)
            {
                sessionHolder.SynchronizedWithTransaction = false;
            }
        }
        /// <summary>
        /// Cleanup resources after transaction completion.
        /// </summary>
        /// <param name="transaction">Transaction object returned by
        /// <see cref="M:Spring.Transaction.Support.AbstractPlatformTransactionManager.DoGetTransaction"/>.</param>
        /// <remarks>
        /// <note>
        /// This implemenation unbinds the SessionFactory and
        /// DbProvider from thread local storage and closes the
        /// ISession.
        /// </note>
        ///     <p>
        /// Called after <see cref="M:Spring.Transaction.Support.AbstractPlatformTransactionManager.DoCommit(Spring.Transaction.Support.DefaultTransactionStatus)"/>
        /// and
        /// <see cref="M:Spring.Transaction.Support.AbstractPlatformTransactionManager.DoRollback(Spring.Transaction.Support.DefaultTransactionStatus)"/>
        /// execution on any outcome.
        /// </p>
        ///     <p>
        /// Should not throw any exceptions but just issue warnings on errors.
        /// </p>
        /// </remarks>
        protected override void DoCleanupAfterCompletion(object transaction)
        {
            HibernateTransactionObject txObject = (HibernateTransactionObject)transaction;

            // Remove the session holder from the thread.
            if (txObject.NewSessionHolder)
            {
                TransactionSynchronizationManager.UnbindResource(SessionFactory);
            }
            // Remove the ADO.NET connection holder from the thread, if exposed.
            if (DbProvider != null)
            {
                TransactionSynchronizationManager.UnbindResource(DbProvider);
            }

            /*
             * try
             * {
             *  //TODO investigate isolation level settings...
             *  //IDbConnection con = txObject.SessionHolder.Session.Connection;
             *  //AdoUtils.ResetConnectionAfterTransaction(con, txObject.PreviousIsolationLevel);
             * }
             * catch (HibernateException ex)
             * {
             *  log.Info("Could not access ADO.NET IDbConnection of Hibernate Session", ex);
             * }
             */
            ISession session = txObject.SessionHolder.Session;

            if (txObject.NewSessionHolder)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Closing Hibernate Session [" + session + "] after transaction");
                }

                SessionFactoryUtils.CloseSessionOrRegisterDeferredClose(session, SessionFactory);
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Not closing pre-bound Hibernate Session [" + session + "] after transaction");
                }
                if (txObject.SessionHolder.AssignedPreviousFlushMode)
                {
                    session.FlushMode = txObject.SessionHolder.PreviousFlushMode;
                }
            }

            txObject.SessionHolder.Clear();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Cleanup resources after transaction completion.
        /// </summary>
        /// <param name="transaction">Transaction object returned by
        /// <see cref="M:Spring.Transaction.Support.AbstractPlatformTransactionManager.DoGetTransaction"/>.</param>
        /// <remarks>
        /// <note>
        /// This implemenation unbinds the SessionFactory and
        /// DbProvider from thread local storage and closes the
        /// ISession.
        /// </note>
        ///     <p>
        /// Called after <see cref="M:Spring.Transaction.Support.AbstractPlatformTransactionManager.DoCommit(Spring.Transaction.Support.DefaultTransactionStatus)"/>
        /// and
        /// <see cref="M:Spring.Transaction.Support.AbstractPlatformTransactionManager.DoRollback(Spring.Transaction.Support.DefaultTransactionStatus)"/>
        /// execution on any outcome.
        /// </p>
        ///     <p>
        /// Should not throw any exceptions but just issue warnings on errors.
        /// </p>
        /// </remarks>
        protected override void DoCleanupAfterCompletion(object transaction)
        {
            HibernateTransactionObject txObject = (HibernateTransactionObject)transaction;

            // Remove the session holder from the thread.
            if (txObject.NewSessionHolder)
            {
                TransactionSynchronizationManager.UnbindResource(SessionFactory);
            }
            // Remove the ADO.NET connection holder from the thread, if exposed.
            if (DbProvider != null)
            {
                TransactionSynchronizationManager.UnbindResource(DbProvider);
            }

            ISession session = txObject.SessionHolder.Session;

            if (txObject.NewSessionHolder)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Closing Hibernate Session [" + session + "] after transaction");
                }
                SessionFactoryUtils.CloseSessionOrRegisterDeferredClose(session, SessionFactory);
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Not closing pre-bound Hibernate Session [" + session + "] after transaction");
                }
                if (txObject.SessionHolder.AssignedPreviousFlushMode)
                {
                    session.FlushMode = txObject.SessionHolder.PreviousFlushMode;
                }
            }
            txObject.SessionHolder.Clear();
        }