public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
        {
            if (session.TransactionContext != null)
            {
                return;
            }

            if (System.Transactions.Transaction.Current == null)
            {
                return;
            }

            var transactionContext = new DistributedTransactionContext(session, System.Transactions.Transaction.Current);

            session.TransactionContext = transactionContext;
            logger.DebugFormat("enlisted into DTC transaction: {0}",
                               transactionContext.AmbientTransation.IsolationLevel);
            session.AfterTransactionBegin(null);

            //use pull-request https://github.com/nhibernate/nhibernate-core/pull/206 (with handler manipulation)

            TransactionCompletedEventHandler handler = null;

            handler =
                delegate(object sender, TransactionEventArgs e)
            {
                lock (session)
                {
                    using (new SessionIdLoggingContext(session.SessionId))
                    {
                        ((DistributedTransactionContext)session.TransactionContext).IsInActiveTransaction = false;

                        bool wasSuccessful = false;
                        try
                        {
                            wasSuccessful = e.Transaction.TransactionInformation.Status
                                            == TransactionStatus.Committed;
                        }
                        catch (ObjectDisposedException ode)
                        {
                            logger.Warn("Completed transaction was disposed, assuming transaction rollback", ode);
                        }
                        session.AfterTransactionCompletion(wasSuccessful, null);

                        if (transactionContext.ShouldCloseSessionOnDistributedTransactionCompleted)
                        {
                            session.CloseSessionFromDistributedTransaction();
                        }
                        session.TransactionContext = null;
                    }

                    transactionContext.AmbientTransation.TransactionCompleted -= handler;
                }
            };

            transactionContext.AmbientTransation.TransactionCompleted += handler;

            transactionContext.AmbientTransation.EnlistVolatile(transactionContext,
                                                                EnlistmentOptions.EnlistDuringPrepareRequired);
        }
		public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
		{
			if (session.TransactionContext != null)
				return;
			if (System.Transactions.Transaction.Current == null)
				return;
			var transactionContext = new DistributedTransactionContext(session, System.Transactions.Transaction.Current);
			session.TransactionContext = transactionContext;
			logger.DebugFormat("enlisted into DTC transaction: {0}", transactionContext.AmbientTransation.IsolationLevel);
			session.AfterTransactionBegin(null);
			transactionContext.AmbientTransation.TransactionCompleted += delegate(object sender, TransactionEventArgs e)
			{
				bool wasSuccessful = false;
				try
				{
					wasSuccessful = e.Transaction.TransactionInformation.Status
					                == TransactionStatus.Committed;
				}
				catch (ObjectDisposedException ode)
				{
					logger.Warn("Completed transaction was disposed, assuming transaction rollback", ode);
				}
				session.AfterTransactionCompletion(wasSuccessful, null);
				if (transactionContext.ShouldCloseSessionOnDistributedTransactionCompleted)
				{
					session.CloseSessionFromDistributedTransaction();
				}
				session.TransactionContext = null;
			};
			transactionContext.AmbientTransation.EnlistVolatile(transactionContext, EnlistmentOptions.EnlistDuringPrepareRequired);
		}
        /// <summary>
        /// Begins the <see cref="DbTransaction"/> on the <see cref="DbConnection"/>
        /// used by the <see cref="ISession"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any problems encountered while trying to create
        /// the <see cref="DbTransaction"/>.
        /// </exception>
        public void Begin(IsolationLevel isolationLevel)
        {
            using (session.BeginProcess())
            {
                if (begun)
                {
                    return;
                }

                if (commitFailed)
                {
                    throw new TransactionException("Cannot restart transaction after failed commit");
                }

                if (isolationLevel == IsolationLevel.Unspecified)
                {
                    isolationLevel = session.Factory.Settings.IsolationLevel;
                }

                log.Debug("Begin ({0})", isolationLevel);

                try
                {
                    if (isolationLevel == IsolationLevel.Unspecified)
                    {
                        trans = session.Connection.BeginTransaction();
                    }
                    else
                    {
                        trans = session.Connection.BeginTransaction(isolationLevel);
                    }
                }
                catch (HibernateException)
                {
                    // Don't wrap HibernateExceptions
                    throw;
                }
                catch (Exception e)
                {
                    log.Error(e, "Begin transaction failed");
                    throw new TransactionException("Begin failed with SQL exception", e);
                }

                begun      = true;
                committed  = false;
                rolledBack = false;

                session.AfterTransactionBegin(this);
                foreach (var dependentSession in session.ConnectionManager.DependentSessions)
                {
                    dependentSession.AfterTransactionBegin(this);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Begins the <see cref="IDbTransaction"/> on the <see cref="IDbConnection"/>
        /// used by the <see cref="ISession"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any problems encountered while trying to create
        /// the <see cref="IDbTransaction"/>.
        /// </exception>
        public void Begin(IsolationLevel isolationLevel)
        {
            using (new SessionIdLoggingContext(sessionId))
            {
                if (begun)
                {
                    return;
                }

                if (commitFailed)
                {
                    throw new TransactionException("Cannot restart transaction after failed commit");
                }

                if (isolationLevel == IsolationLevel.Unspecified)
                {
                    isolationLevel = session.Factory.Settings.IsolationLevel;
                }

                log.Debug(string.Format("Begin ({0})", isolationLevel));

                try
                {
                    if (System.Transactions.Transaction.Current == null)
                    {
                        if (isolationLevel == IsolationLevel.Unspecified)
                        {
                            trans = session.Connection.BeginTransaction();
                        }
                        else
                        {
                            trans = session.Connection.BeginTransaction(isolationLevel);
                        }
                    }
                }
                catch (HibernateException)
                {
                    // Don't wrap HibernateExceptions
                    throw;
                }
                catch (Exception e)
                {
                    log.Error("Begin transaction failed", e);
                    throw new TransactionException("Begin failed with SQL exception", e);
                }

                begun      = true;
                committed  = false;
                rolledBack = false;

                session.AfterTransactionBegin(this);
            }
        }
Exemple #5
0
        /// <summary>
        /// Begins the <see cref="IDbTransaction"/> on the <see cref="IDbConnection"/>
        /// used by the <see cref="ISession"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any problems encountered while trying to create
        /// the <see cref="IDbTransaction"/>.
        /// </exception>
        public void Begin(IsolationLevel isolationLevel)
        {
            if (begun)
            {
                return;
            }

            if (commitFailed)
            {
                throw new TransactionException("Cannot restart transaction after failed commit");
            }

            log.Debug("begin");

            try
            {
                if (isolationLevel == IsolationLevel.Unspecified)
                {
                    isolationLevel = session.Factory.Isolation;
                }

                if (isolationLevel == IsolationLevel.Unspecified)
                {
                    trans = session.Connection.BeginTransaction();
                }
                else
                {
                    trans = session.Connection.BeginTransaction(isolationLevel);
                }
            }
            catch (HibernateException)
            {
                // Don't wrap HibernateExceptions
                throw;
            }
            catch (Exception e)
            {
                log.Error("Begin transaction failed", e);
                throw new TransactionException("Begin failed with SQL exception", e);
            }

            begun      = true;
            committed  = false;
            rolledBack = false;

            session.AfterTransactionBegin(this);
        }
        public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
        {
            if (session.TransactionContext != null)
            {
                return;
            }

            if (System.Transactions.Transaction.Current == null)
            {
                return;
            }

            var transactionContext = new DistributedTransactionContext(session,
                                                                       System.Transactions.Transaction.Current);

            session.TransactionContext = transactionContext;
            logger.DebugFormat("enlisted into DTC transaction: {0}",
                               transactionContext.AmbientTransation.IsolationLevel);
            session.AfterTransactionBegin(null);
            transactionContext.AmbientTransation.TransactionCompleted +=
                delegate(object sender, TransactionEventArgs e)
            {
                using (new SessionIdLoggingContext(session.SessionId))
                {
                    bool wasSuccessful = false;
                    try
                    {
                        wasSuccessful = e.Transaction.TransactionInformation.Status
                                        == TransactionStatus.Committed;
                    }
                    catch (ObjectDisposedException ode)
                    {
                        logger.Warn("Completed transaction was disposed, assuming transaction rollback", ode);
                    }
                    session.AfterTransactionCompletion(wasSuccessful, null);
                    if (transactionContext.ShouldCloseSessionOnDistributedTransactionCompleted)
                    {
                        session.CloseSessionFromDistributedTransaction();
                    }
                    session.TransactionContext = null;
                }
            };
            transactionContext.AmbientTransation.EnlistVolatile(transactionContext,
                                                                EnlistmentOptions.EnlistDuringPrepareRequired);
        }
Exemple #7
0
 private void EnlistDependentSession(ISessionImplementor dependentSession, ITransactionContext mainContext)
 {
     dependentSession.TransactionContext = CreateDependentContext(dependentSession, mainContext);
     dependentSession.AfterTransactionBegin(null);
 }
        public void EnlistInDistributedTransactionIfNeeded(ISessionImplementor session)
        {
            if (session.TransactionContext != null)
            {
                return;
            }

            if (System.Transactions.Transaction.Current == null)
            {
                return;
            }

            var originatingSession = session.ConnectionManager.Session;

            if (originatingSession != session)
            {
                session.TransactionContext = new DependentContext();
            }

            if (originatingSession.TransactionContext != null)
            {
                return;
            }

            session = originatingSession;

            var transactionContext = new DistributedTransactionContext(session,
                                                                       System.Transactions.Transaction.Current);

            session.TransactionContext = transactionContext;
            logger.DebugFormat("enlisted into DTC transaction: {0}",
                               transactionContext.AmbientTransation.IsolationLevel);
            session.AfterTransactionBegin(null);
            foreach (var dependentSession in session.ConnectionManager.DependentSessions)
            {
                dependentSession.AfterTransactionBegin(null);
            }

            TransactionCompletedEventHandler handler = null;

            handler = delegate(object sender, TransactionEventArgs e)
            {
                using (new SessionIdLoggingContext(session.SessionId))
                {
                    ((DistributedTransactionContext)session.TransactionContext).IsInActiveTransaction = false;

                    bool wasSuccessful = false;
                    try
                    {
                        wasSuccessful = e.Transaction.TransactionInformation.Status
                                        == TransactionStatus.Committed;
                    }
                    catch (ObjectDisposedException ode)
                    {
                        logger.Warn("Completed transaction was disposed, assuming transaction rollback", ode);
                    }
                    session.ConnectionManager.AfterTransaction();
                    session.AfterTransactionCompletion(wasSuccessful, null);
                    foreach (var dependentSession in session.ConnectionManager.DependentSessions)
                    {
                        dependentSession.AfterTransactionCompletion(wasSuccessful, null);
                    }

                    Cleanup(session);
                }

                e.Transaction.TransactionCompleted -= handler;
            };

            transactionContext.AmbientTransation.TransactionCompleted += handler;

            transactionContext.AmbientTransation.EnlistVolatile(transactionContext,
                                                                EnlistmentOptions.EnlistDuringPrepareRequired);
        }
Exemple #9
0
 public void AfterTransactionBegin(ITransaction tx)
 {
     _session.AfterTransactionBegin(tx);
 }
 public void AfterTransactionBegin(ITransaction tx)
 {
     delegat.AfterTransactionBegin(tx);
 }