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 MsmqDefaultLockingQueue(string formatName, int accessMode)
     : base(formatName, accessMode)
 {
     lockMap = new Dictionary<long, TransactionLookupEntry>();
     dtcTransMap = new Dictionary<Guid, List<long>>();
     this.internalStateLock = new object();
     transactionCompletedHandler = new TransactionCompletedEventHandler(Current_TransactionCompleted);
 }
 public MsmqDefaultLockingQueue(string formatName, int accessMode)
     : base(formatName, accessMode)
 {
     lockMap                     = new Dictionary <long, TransactionLookupEntry>();
     dtcTransMap                 = new Dictionary <Guid, List <long> >();
     this.internalStateLock      = new object();
     transactionCompletedHandler = new TransactionCompletedEventHandler(Current_TransactionCompleted);
 }
Example #4
0
        private void FireCompleted()
        {
            TransactionCompletedEventHandler handler = this.TransactionCompleted;

            if (handler != null)
            {
                handler(this, new TransactionEventArgs(this));
            }
        }
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     if (transactionCompletedDelegate != null)
     {
         TransactionEventArgs e = new TransactionEventArgs {
             transaction = tx.outcomeSource.InternalClone()
         };
         transactionCompletedDelegate(e.transaction, e);
     }
 }
 public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
 {
     this.queue = queue;
     this.receiveContextSettings = receiveContextSettings;
     this.messageExpiryMap = new Dictionary<long, MsmqReceiveContext>();
     this.transMessages = new Dictionary<Guid, List<MsmqReceiveContext>>();
     this.transactionCompletedHandler = new TransactionCompletedEventHandler(this.OnTransactionCompleted);
     this.messageExpiryTimer = new IOThreadTimer(new Action<object>(this.CleanupExpiredLocks), null, false);
     this.messageExpiryTimer.Set(this.messageTimeoutInterval);
 }
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     if (transactionCompletedDelegate != null)
     {
         TransactionEventArgs e = new TransactionEventArgs {
             transaction = tx.outcomeSource.InternalClone()
         };
         transactionCompletedDelegate(e.transaction, e);
     }
 }
 public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
 {
     this.queue = queue;
     this.receiveContextSettings      = receiveContextSettings;
     this.messageExpiryMap            = new Dictionary <long, MsmqReceiveContext>();
     this.transMessages               = new Dictionary <Guid, List <MsmqReceiveContext> >();
     this.transactionCompletedHandler = new TransactionCompletedEventHandler(this.OnTransactionCompleted);
     this.messageExpiryTimer          = new IOThreadTimer(new Action <object>(this.CleanupExpiredLocks), null, false);
     this.messageExpiryTimer.Set(this.messageTimeoutInterval);
 }
Example #9
0
        // Fire completion to anyone registered for outcome
        internal void FireCompletion( )
        {
            TransactionCompletedEventHandler eventHandlers = this.transactionCompletedDelegate;

            if (eventHandlers != null)
            {
                TransactionEventArgs args = new TransactionEventArgs();
                args.transaction = this.outcomeSource.InternalClone();

                eventHandlers(args.transaction, args);
            }
        }
        internal void FireCompletion()
        {
            TransactionCompletedEventHandler transactionCompletedDelegate = this.transactionCompletedDelegate;

            if (transactionCompletedDelegate != null)
            {
                TransactionEventArgs e = new TransactionEventArgs {
                    transaction = this.outcomeSource.InternalClone()
                };
                transactionCompletedDelegate(e.transaction, e);
            }
        }
        public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
        {
            Fx.Assert(queue is ILockingQueue, "Queue must be ILockingQueue");

            this.disposed = false;
            this.queue    = queue;
            this.receiveContextSettings = receiveContextSettings;
            this.messageExpiryMap       = new Dictionary <long, MsmqReceiveContext>();
            this.transMessages          = new Dictionary <Guid, List <MsmqReceiveContext> >();
            transactionCompletedHandler = new TransactionCompletedEventHandler(OnTransactionCompleted);

            this.messageExpiryTimer = new IOThreadTimer(new Action <object>(CleanupExpiredLocks), null, false);
            this.messageExpiryTimer.Set(messageTimeoutInterval);
        }
        public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
        {
            Fx.Assert(queue is ILockingQueue, "Queue must be ILockingQueue");

            this.disposed = false;
            this.queue = queue;
            this.receiveContextSettings = receiveContextSettings;
            this.messageExpiryMap = new Dictionary<long, MsmqReceiveContext>();
            this.transMessages = new Dictionary<Guid, List<MsmqReceiveContext>>();
            transactionCompletedHandler = new TransactionCompletedEventHandler(OnTransactionCompleted);

            this.messageExpiryTimer = new IOThreadTimer(new Action<object>(CleanupExpiredLocks), null, false);
            this.messageExpiryTimer.Set(messageTimeoutInterval);
        }
        public BatchManager(BeginBatchedDelegate <TItem> batchedBeginOperation, EndBatchedDelegate batchedEndOperation, CalculateBatchSizeDelegate <TItem> calculateBatchSize, OnRetryDelegate <TItem> onRetryOperation, TransactionStateChangedDelegate transactionStateChanged, TransactionCompletedEventHandler transactionCompletedEventHandler, long flushThreshold, long maximumBatchSize, int batchOverheadSize)
        {
            this.StartBatchOperation = new BatchManager <TItem> .StartBatchOperationDelegate(BatchManager <TItem> .PerformFlushAsyncResult.StartBatchOperation);

            this.BatchedBegin                     = batchedBeginOperation;
            this.BatchedEnd                       = batchedEndOperation;
            this.CalculateBatchSize               = calculateBatchSize;
            this.OnRetry                          = onRetryOperation;
            this.OnTransactionStateChanged        = transactionStateChanged;
            this.transactionCompletedEventHandler = transactionCompletedEventHandler;
            this.batchedResults                   = new Queue <BatchManager <TItem> .BatchedObjectsAsyncResult>();
            this.transactionalBatchResults        = new Dictionary <string, Queue <BatchManager <TItem> .BatchedObjectsAsyncResult> >();
            this.flushTimer                       = new IOThreadTimer(BatchManager <TItem> .flushTimerCallback, this, false);
            this.flushThreshold                   = flushThreshold;
            this.maximumBatchSize                 = maximumBatchSize;
            this.pendingBatchSize                 = batchOverheadSize;
            this.batchOverheadSize                = batchOverheadSize;
            this.syncLock                         = new object();
            this.syncTransactionLock              = new object();
        }
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     tx.promoteState.EnterState(tx);
     tx.State.AddOutcomeRegistrant(tx, transactionCompletedDelegate);
 }
Example #15
0
 internal virtual void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     throw TransactionException.CreateTransactionStateException(System.Transactions.SR.GetString("TraceSourceLtm"), tx.innerException);
 }
Example #16
0
 internal virtual void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     throw TransactionException.CreateTransactionStateException(tx._innerException, tx.DistributedTxId);
 }
Example #17
0
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     // Add this guy to the list of people to be notified of the outcome.
     tx._transactionCompletedDelegate = (TransactionCompletedEventHandler)
         System.Delegate.Combine(tx._transactionCompletedDelegate, transactionCompletedDelegate);
 }
 internal virtual void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     throw TransactionException.CreateTransactionStateException(System.Transactions.SR.GetString("TraceSourceLtm"), tx.innerException);
 }
 public HttpAcknowledgementReceiveContext(RequestContext requestContext)
 {
     this.requestContext = requestContext;
     transactionCompletedHandler = new TransactionCompletedEventHandler(OnTransactionCompleted);
 }
 public HttpAcknowledgementReceiveContext(RequestContext requestContext)
 {
     this.requestContext         = requestContext;
     transactionCompletedHandler = new TransactionCompletedEventHandler(OnTransactionCompleted);
 }
 public BatchManager(BeginBatchedDelegate <TItem> batchedBeginOperation, EndBatchedDelegate batchedEndOperation, CalculateBatchSizeDelegate <TItem> calculateBatchSize, OnRetryDelegate <TItem> onRetryOperation, TransactionStateChangedDelegate transactionStateChanged, TransactionCompletedEventHandler transactionCompletedEventHandler, long flushThreshold, long maximumBatchSize) : this(batchedBeginOperation, batchedEndOperation, calculateBatchSize, onRetryOperation, transactionStateChanged, transactionCompletedEventHandler, flushThreshold, maximumBatchSize, 0)
 {
 }
Example #22
0
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     tx._promoteState.EnterState(tx);
     tx.State.AddOutcomeRegistrant(tx, transactionCompletedDelegate);
 }
Example #23
0
 public TransactionEventHandlerThunk(TransactionCompletedEventHandler callback)
 {
     this.callback = callback;
 }
Example #24
0
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     if (transactionCompletedDelegate != null)
     {
         TransactionEventArgs args = new TransactionEventArgs();
         args._transaction = tx._outcomeSource.InternalClone();
         transactionCompletedDelegate(args._transaction, args);
     }
 }
Example #25
0
 public RemovedOperation(Action doAction, Dictionary <string, byte[]> deletedObject, TransactionCompletedEventHandler handler) : base(doAction, () =>
 {
     foreach (KeyValuePair <string, byte[]> action in RollBackCollection)
     {
         if (action.Value == null)
         {
             Directory.CreateDirectory(action.Key);
             continue;
         }
         using (var stream = File.Create(action.Key, action.Value.Length))
         {
             stream.Write(action.Value, 0, action.Value.Length);
         }
     }
 }, handler)
 {
     RollBackCollection = deletedObject;
 }
Example #26
0
 internal virtual void AddOutcomeRegistrant( InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate )
 {
     throw TransactionException.CreateTransactionStateException( SR.GetString( SR.TraceSourceLtm ), tx.innerException, tx.DistributedTxId);
 }
Example #27
0
 public TransactedOperation(Action doAction, Action rollBackAction, TransactionCompletedEventHandler handler)
 {
     MainAction     = doAction;
     RollBackAction = rollBackAction;
     Handler        = handler;
 }
Example #28
0
 public static TransactionCompletedEventHandler ThunkTransactionEventHandler(TransactionCompletedEventHandler handler)
 {
     return((new TransactionEventHandlerThunk(handler)).ThunkFrame);
 }
        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);
        }
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     tx.transactionCompletedDelegate = (TransactionCompletedEventHandler) Delegate.Combine(tx.transactionCompletedDelegate, transactionCompletedDelegate);
 }
 internal override void AddOutcomeRegistrant(InternalTransaction tx, TransactionCompletedEventHandler transactionCompletedDelegate)
 {
     tx.transactionCompletedDelegate = (TransactionCompletedEventHandler)Delegate.Combine(tx.transactionCompletedDelegate, transactionCompletedDelegate);
 }