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);
		}
        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);
        }
Beispiel #3
0
            void End(bool wasSuccessful)
            {
                using (new SessionIdLoggingContext(session.SessionId))
                {
                    ((DistributedTransactionContext)session.TransactionContext).IsInActiveTransaction = false;

                    session.AfterTransactionCompletion(wasSuccessful, null);

                    if (ShouldCloseSessionOnDistributedTransactionCompleted)
                    {
                        session.CloseSessionFromDistributedTransaction();
                    }

                    session.TransactionContext = null;
                }
            }
        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);
        }
 private static void Cleanup(ISessionImplementor session)
 {
     foreach (var dependentSession in session.ConnectionManager.DependentSessions.ToList())
     {
         if (dependentSession.TransactionContext?.ShouldCloseSessionOnDistributedTransactionCompleted ?? false)
         {
             // This change the enumerated collection.
             dependentSession.CloseSessionFromDistributedTransaction();
         }
         dependentSession.TransactionContext?.Dispose();
         dependentSession.TransactionContext = null;
     }
     if (session.TransactionContext.ShouldCloseSessionOnDistributedTransactionCompleted)
     {
         session.CloseSessionFromDistributedTransaction();
     }
     session.TransactionContext.Dispose();
     session.TransactionContext = null;
 }
 public void CloseSessionFromDistributedTransaction()
 {
     delegat.CloseSessionFromDistributedTransaction();
 }