/// <summary>
		/// Initializes a new instance of the <see cref="SpringSessionSynchronization"/> class.
        /// </summary>
        public 	SpringSessionSynchronization(SessionHolder sessionHolder, ISessionFactory sessionFactory,
               	                             IAdoExceptionTranslator adoExceptionTranslator, bool newSession)
		{
            this.sessionHolder = sessionHolder;
            this.sessionFactory = sessionFactory;
            this.adoExceptionTranslator = adoExceptionTranslator;
            this.newSession = newSession;
		}
Exemplo n.º 2
0
        /// <summary>
        /// Return whether the given Hibernate Session is transactional, that is,
        /// bound to the current thread by Spring's transaction facilities.
        /// </summary>
        /// <param name="session">The hibernate session to check</param>
        /// <param name="sessionFactory">The session factory that the session
        /// was created with, can be null.</param>
        /// <returns>
        ///     <c>true</c> if the session transactional; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsSessionTransactional(ISession session, ISessionFactory sessionFactory)
        {
            if (sessionFactory == null)
            {
                return(false);
            }
            SessionHolder sessionHolder =
                (SessionHolder)TransactionSynchronizationManager.GetResource(sessionFactory);

            return(sessionHolder != null && sessionHolder.ContainsSession(session));
        }
Exemplo n.º 3
0
        public object DoInTransaction(ITransactionStatus status)
        {
            SessionHolder holder = (SessionHolder)TransactionSynchronizationManager.GetResource(sf);

            Assert.IsNotNull(holder, "Has thread session");
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
            Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
            tt.PropagationBehavior = TransactionPropagation.NotSupported;
            tt.Execute(new NotSupportedTxCallback(sf));

            Assert.IsTrue(holder.Session == SessionFactoryUtils.GetSession(sf, false), "Same thread session as before");
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
            Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
            return(null);
        }
        /// <summary>
        /// Suspend the resources of the current transaction.
        /// </summary>
        /// <param name="transaction">
        /// Transaction object returned by
        /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoGetTransaction"/>.
        /// </param>
        /// <returns>
        /// An object that holds suspended resources (will be kept unexamined for passing it into
        /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoResume"/>.)
        /// </returns>
        /// <remarks>
        /// Transaction synchronization will already have been suspended.
        /// </remarks>
        /// <exception cref="Spring.Transaction.IllegalTransactionStateException">
        /// If suspending is not supported by the transaction manager implementation.
        /// </exception>
        /// <exception cref="Spring.Transaction.TransactionException">
        /// in case of system errors.
        /// </exception>
        protected override object DoSuspend(object transaction)
        {
            HibernateTransactionObject txObject = (HibernateTransactionObject)transaction;

            txObject.SetSessionHolder(null, false);
            SessionHolder sessionHolder =
                (SessionHolder)TransactionSynchronizationManager.UnbindResource(SessionFactory);
            ConnectionHolder connectionHolder = null;

            if (DbProvider != null)
            {
                connectionHolder = (ConnectionHolder)TransactionSynchronizationManager.UnbindResource(DbProvider);
            }
            return(new SuspendedResourcesHolder(sessionHolder, connectionHolder));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Applies the current transaction timeout, if any, to the given
        /// criteria object
        /// </summary>
        /// <param name="criteria">The Hibernate Criteria object.</param>
        /// <param name="sessionFactory">Hibernate SessionFactory that the Criteria was created for
        /// (can be <code>null</code>).</param>
        /// <exception cref="ArgumentNullException">If criteria argument is null.</exception>
        public static void ApplyTransactionTimeout(ICriteria criteria, ISessionFactory sessionFactory)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException("criteria", "No Criteria object specified");
            }

            SessionHolder sessionHolder =
                (SessionHolder)TransactionSynchronizationManager.GetResource(sessionFactory);

            if (sessionHolder != null && sessionHolder.HasTimeout)
            {
                criteria.SetTimeout(sessionHolder.TimeToLiveInSeconds);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Applies the current transaction timeout, if any, to the given
 /// Hibenrate query object.
 /// </summary>
 /// <param name="query">The Hibernate Query object.</param>
 /// <param name="sessionFactory">Hibernate SessionFactory that the Query was created for
 /// (can be <code>null</code>).</param>
 /// <exception cref="ArgumentNullException">If query argument is null.</exception>
 public static void ApplyTransactionTimeout(IQuery query, ISessionFactory sessionFactory)
 {
     if (query == null)
     {
         throw new ArgumentNullException("queryObject", "No query object specified");
     }
     if (sessionFactory != null)
     {
         SessionHolder sessionHolder =
             (SessionHolder)TransactionSynchronizationManager.GetResource(sessionFactory);
         if (sessionHolder != null && sessionHolder.HasTimeout)
         {
             query.SetTimeout(sessionHolder.TimeToLiveInSeconds);
         }
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Return the current transaction object.
        /// </summary>
        /// <returns>The current transaction object.</returns>
        /// <exception cref="Spring.Transaction.CannotCreateTransactionException">
        /// If transaction support is not available.
        /// </exception>
        /// <exception cref="Spring.Transaction.TransactionException">
        /// In the case of lookup or system errors.
        /// </exception>
        protected override object DoGetTransaction()
        {
            HibernateTransactionObject txObject = new HibernateTransactionObject();

            txObject.SavepointAllowed = NestedTransactionsAllowed;
            if (TransactionSynchronizationManager.HasResource(SessionFactory))
            {
                SessionHolder sessionHolder =
                    (SessionHolder)TransactionSynchronizationManager.GetResource(SessionFactory);
                if (log.IsDebugEnabled)
                {
                    log.Debug("Found thread-bound Session [" + sessionHolder.Session +
                              "] for Hibernate transaction");
                }
                txObject.SetSessionHolder(sessionHolder, false);
                if (DbProvider != null)
                {
                    ConnectionHolder conHolder = (ConnectionHolder)
                                                 TransactionSynchronizationManager.GetResource(DbProvider);
                    txObject.ConnectionHolder = conHolder;
                }
            }
            return(txObject);
        }
Exemplo n.º 8
0
 public RequiresNewTxCallbackInner(SessionHolder holder)
 {
     this.holder = holder;
 }
Exemplo n.º 9
0
 public RequiresNewTxCallback(ISessionFactory sf, SessionHolder holder)
 {
     this.holder = holder;
     this.sf     = sf;
 }
 public void SetSessionHolder(SessionHolder sessionHolder, bool newSessionHolder)
 {
     this.sessionHolder    = sessionHolder;
     this.newSessionHolder = newSessionHolder;
 }
 public SuspendedResourcesHolder(SessionHolder sessionHolder, ConnectionHolder conHolder)
 {
     this.sessionHolder    = sessionHolder;
     this.connectionHolder = conHolder;
 }
 public SuspendedResourcesHolder(SessionHolder sessionHolder, ConnectionHolder conHolder)
 {
     this.sessionHolder = sessionHolder;
     this.connectionHolder = conHolder;
 }
 public void SetSessionHolder(SessionHolder sessionHolder, bool newSessionHolder)
 {
     this.sessionHolder = sessionHolder;
     this.newSessionHolder = newSessionHolder;
 }
Exemplo n.º 14
0
        private static ISession DoGetSession(
            ISessionFactory sessionFactory, IInterceptor entityInterceptor,
            IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate)
        {
            AssertUtils.ArgumentNotNull(sessionFactory, "sessionFactory", "SessionFactory can not be null");

            SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.GetResource(sessionFactory);

            if (sessionHolder != null && !sessionHolder.IsEmpty)
            {
                // pre-bound Hibernate Session
                ISession session = null;
                if (TransactionSynchronizationManager.SynchronizationActive &&
                    sessionHolder.DoesNotHoldNonDefaultSession)
                {
                    // Spring transaction management is active ->
                    // register pre-bound Session with it for transactional flushing.
                    session = sessionHolder.ValidatedSession;
                    if (!sessionHolder.SynchronizedWithTransaction)
                    {
                        log.Debug("Registering Spring transaction synchronization for existing Hibernate Session");
                        TransactionSynchronizationManager.RegisterSynchronization(
                            new SpringSessionSynchronization(sessionHolder, sessionFactory, adoExceptionTranslator, false));
                        sessionHolder.SynchronizedWithTransaction = true;
                        // Switch to FlushMode.AUTO if we're not within a read-only transaction.
                        FlushMode flushMode = session.FlushMode;
                        if (FlushMode.Never == flushMode &&
                            !TransactionSynchronizationManager.CurrentTransactionReadOnly)
                        {
                            session.FlushMode = FlushMode.Auto;
                            sessionHolder.PreviousFlushMode = flushMode;
                        }
                    }
                }
                else
                {
                    // No Spring transaction management active -> simply return default thread-bound Session, if any
                    // (possibly from OpenSessionInViewModule)
                    session = sessionHolder.ValidatedSession;
                }

                if (session != null)
                {
                    return(session);
                }
            }


            ISession sess = OpenSession(sessionFactory, entityInterceptor);

            // Set Session to FlushMode.Never if we're within a read-only transaction.
            // Use same Session for further Hibernate actions within the transaction.
            // Thread object will get removed by synchronization at transaction completion.
            if (TransactionSynchronizationManager.SynchronizationActive)
            {
                log.Debug("Registering Spring transaction synchronization for new Hibernate Session");
                SessionHolder holderToUse = sessionHolder;
                if (holderToUse == null)
                {
                    holderToUse = new SessionHolder(sess);
                }
                else
                {
                    holderToUse.AddSession(sess);
                }
                if (TransactionSynchronizationManager.CurrentTransactionReadOnly)
                {
                    sess.FlushMode = FlushMode.Never;
                }
                TransactionSynchronizationManager.RegisterSynchronization(
                    new SpringSessionSynchronization(holderToUse, sessionFactory, adoExceptionTranslator, true));
                holderToUse.SynchronizedWithTransaction = true;
                if (holderToUse != sessionHolder)
                {
                    TransactionSynchronizationManager.BindResource(sessionFactory, holderToUse);
                }
            }



            // Check whether we are allowed to return the Session.
            if (!allowCreate && !IsSessionTransactional(sess, sessionFactory))
            {
                CloseSession(sess);
                throw new InvalidOperationException("No Hibernate Session bound to thread, " +
                                                    "and configuration does not allow creation of non-transactional one here");
            }

            return(sess);
        }
 public RequiresNewTxCallback(ISessionFactory sf, SessionHolder holder)
 {
     this.holder = holder;
     this.sf = sf;
 }
 public RequiresNewTxCallbackInner(SessionHolder holder)
 {
     this.holder = holder;
 }
Exemplo n.º 17
0
        private static ISession DoGetSession(
            ISessionFactory sessionFactory, IInterceptor entityInterceptor,
            IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate)
        {
            AssertUtils.ArgumentNotNull(sessionFactory, "sessionFactory", "SessionFactory can not be null");

            SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.GetResource(sessionFactory);
            if (sessionHolder != null && !sessionHolder.IsEmpty) 
            {
                // pre-bound Hibernate Session
                ISession session = null;
                if (TransactionSynchronizationManager.SynchronizationActive &&
                    sessionHolder.DoesNotHoldNonDefaultSession)
                {
                    // Spring transaction management is active ->
                    // register pre-bound Session with it for transactional flushing.
                    session = sessionHolder.ValidatedSession;
                    if (session != null && !sessionHolder.SynchronizedWithTransaction) 
                    {
                        log.Debug("Registering Spring transaction synchronization for existing Hibernate Session");
                        TransactionSynchronizationManager.RegisterSynchronization(
                            new SpringSessionSynchronization(sessionHolder, sessionFactory, adoExceptionTranslator, false));
                        sessionHolder.SynchronizedWithTransaction = true;
                        // Switch to FlushMode.AUTO if we're not within a read-only transaction.
                        FlushMode flushMode = session.FlushMode;
                        if (FlushMode.Never == flushMode &&
                            !TransactionSynchronizationManager.CurrentTransactionReadOnly) 
                        {
                            session.FlushMode = FlushMode.Auto;
                            sessionHolder.PreviousFlushMode = flushMode;
                        }
                    }
                }
                else
                {
                   // No Spring transaction management active -> simply return default thread-bound Session, if any
                   // (possibly from OpenSessionInViewModule)
                    session = sessionHolder.ValidatedSession;
                }
                
                if (session != null) 
                {
                    return session;
                }
                
            }


            ISession sess = OpenSession(sessionFactory, entityInterceptor);
            // Set Session to FlushMode.Never if we're within a read-only transaction.
            // Use same Session for further Hibernate actions within the transaction.
            // Thread object will get removed by synchronization at transaction completion.
            if (TransactionSynchronizationManager.SynchronizationActive) 
            {
                log.Debug("Registering Spring transaction synchronization for new Hibernate Session");
                SessionHolder holderToUse = sessionHolder;
                if (holderToUse == null) 
                {
                    holderToUse = new SessionHolder(sess);
                }
                else 
                {
                    holderToUse.AddSession(sess);
                }
                if (TransactionSynchronizationManager.CurrentTransactionReadOnly) 
                {
                    sess.FlushMode = FlushMode.Never;
                }
                TransactionSynchronizationManager.RegisterSynchronization(
                    new SpringSessionSynchronization(holderToUse, sessionFactory, adoExceptionTranslator, true));
                holderToUse.SynchronizedWithTransaction = true;
                if (holderToUse != sessionHolder) 
                {
                    TransactionSynchronizationManager.BindResource(sessionFactory, holderToUse);
                }
            }

            

            // Check whether we are allowed to return the Session.
            if (!allowCreate && !IsSessionTransactional(sess, sessionFactory)) 
            {
                CloseSession(sess);
                throw new InvalidOperationException ("No Hibernate Session bound to thread, " +
                    "and configuration does not allow creation of non-transactional one here");
            }

            return sess;


        }