Example #1
0
            /// <summary>
            /// Prepare the session for the transaction commit. Run
            /// <see cref="ISessionImplementor.BeforeTransactionCompletion(ITransaction)"/> for the session and for
            /// <see cref="ConnectionManager.DependentSessions"/> if any. <see cref="Lock"/> the context
            /// before signaling it is done, or before rollback in case of failure.
            /// </summary>
            /// <param name="preparingEnlistment">The object for notifying the prepare phase outcome.</param>
            public virtual void Prepare(PreparingEnlistment preparingEnlistment)
            {
                _preparing = true;
                using (_session.BeginContext())
                {
                    try
                    {
                        using (_session.ConnectionManager.BeginProcessingFromSystemTransaction(_useConnectionOnSystemTransactionPrepare))
                        {
                            if (_useConnectionOnSystemTransactionPrepare)
                            {
                                // Ensure any newly acquired connection gets enlisted in the transaction. When distributed,
                                // this code runs from another thread and we cannot rely on Transaction.Current.
                                using (var tx = new TransactionScope(EnlistedTransaction))
                                {
                                    // Required when both connection auto-enlistment and session auto-enlistment are disabled.
                                    _session.JoinTransaction();
                                    _session.BeforeTransactionCompletion(null);
                                    foreach (var dependentSession in _session.ConnectionManager.DependentSessions)
                                    {
                                        dependentSession.BeforeTransactionCompletion(null);
                                    }

                                    tx.Complete();
                                }
                            }
                            else
                            {
                                _session.BeforeTransactionCompletion(null);
                                foreach (var dependentSession in _session.ConnectionManager.DependentSessions)
                                {
                                    dependentSession.BeforeTransactionCompletion(null);
                                }
                            }
                        }
                        // Lock the session to ensure second phase gets done before the session is used by code following
                        // the transaction scope disposal.
                        Lock();

                        _logger.Debug("Prepared for system transaction");
                        _preparing = false;
                        preparingEnlistment.Prepared();
                    }
                    catch (Exception exception)
                    {
                        _preparing = false;
                        _logger.Error(exception, "System transaction prepare phase failed");
                        try
                        {
                            CompleteTransaction(false);
                        }
                        finally
                        {
                            preparingEnlistment.ForceRollback(exception);
                        }
                    }
                }
            }
Example #2
0
            void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
            {
                using (new SessionIdLoggingContext(sessionImplementor.SessionId))
                {
                    try
                    {
                        using (var tx = new TransactionScope(AmbientTransation))
                        {
                            sessionImplementor.BeforeTransactionCompletion(null);
                            //if (sessionImplementor.FlushMode != FlushMode.Never && sessionImplementor.ConnectionManager.IsConnected)
                            //{
                            //    using (sessionImplementor.ConnectionManager.FlushingFromDtcTransaction)
                            //    {
                            //        logger.Debug(string.Format("[session-id={0}] Flushing from Dtc Transaction", sessionImplementor.SessionId));
                            //        sessionImplementor.Flush();
                            //    }
                            //}
                            logger.Debug("prepared for DTC transaction");

                            tx.Complete();
                        }
                        preparingEnlistment.Prepared();
                    }
                    catch (Exception exception)
                    {
                        logger.Error("DTC transaction prepre phase failed", exception);
                        preparingEnlistment.ForceRollback(exception);
                    }
                }
            }
            void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
            {
                using (new SessionIdLoggingContext(sessionImplementor.SessionId))
                {
                    try
                    {
                        using (var tx = new TransactionScope(AmbientTransation))
                        {
                            if (sessionImplementor.ConnectionManager.IsConnected)
                            {
                                using (sessionImplementor.ConnectionManager.FlushingFromDtcTransaction)
                                {
                                    sessionImplementor.BeforeTransactionCompletion(null);
                                    foreach (var dependentSession in sessionImplementor.ConnectionManager.DependentSessions)
                                    {
                                        dependentSession.BeforeTransactionCompletion(null);
                                    }

                                    logger.Debug("prepared for DTC transaction");

                                    tx.Complete();
                                }
                            }
                        }
                        preparingEnlistment.Prepared();
                    }
                    catch (Exception exception)
                    {
                        logger.Error("DTC transaction prepare phase failed", exception);
                        preparingEnlistment.ForceRollback(exception);
                    }
                }
            }
Example #4
0
        /// <summary>
        /// Commits the <see cref="ITransaction"/> by flushing the <see cref="ISession"/>
        /// and committing the <see cref="IDbTransaction"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any exception while trying to call <c>Commit()</c> on
        /// the underlying <see cref="IDbTransaction"/>.
        /// </exception>
        public void Commit()
        {
            using (new SessionIdLoggingContext(sessionId))
            {
                CheckNotDisposed();
                CheckBegun();
                CheckNotZombied();

                log.Debug("Start Commit");

                //if (session.FlushMode != FlushMode.Never)
                //{
                //    session.Flush();
                //}

                NotifyLocalSynchsBeforeTransactionCompletion();
                session.BeforeTransactionCompletion(this);

                try
                {
                    if (trans != null)
                    {
                        trans.Commit();
                        log.Debug("IDbTransaction Committed");
                    }

                    committed = true;
                    AfterTransactionCompletion(true);
                    Dispose();

                    log.Debug("Committed");
                }
                catch (HibernateException e)
                {
                    log.Error("Commit failed", e);
                    AfterTransactionCompletion(false);
                    commitFailed = true;
                    // Don't wrap HibernateExceptions
                    throw;
                }
                catch (Exception e)
                {
                    log.Error("Commit failed", e);
                    AfterTransactionCompletion(false);
                    commitFailed = true;
                    throw new TransactionException("Commit failed with SQL exception", e);
                }
                finally
                {
                    CloseIfRequired();
                }
            }
        }
        /// <summary>
        /// Commits the <see cref="ITransaction"/> by flushing the <see cref="ISession"/>
        /// and committing the <see cref="DbTransaction"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any exception while trying to call <c>Commit()</c> on
        /// the underlying <see cref="DbTransaction"/>.
        /// </exception>
        public void Commit()
        {
            using (session.BeginProcess())
            {
                CheckNotDisposed();
                CheckBegun();
                CheckNotZombied();

                log.Debug("Start Commit");

                session.BeforeTransactionCompletion(this);
                NotifyLocalSynchsBeforeTransactionCompletion();
                foreach (var dependentSession in session.ConnectionManager.DependentSessions)
                {
                    dependentSession.BeforeTransactionCompletion(this);
                }

                try
                {
                    trans.Commit();
                    log.Debug("DbTransaction Committed");

                    committed = true;
                    AfterTransactionCompletion(true);
                    Dispose();
                }
                catch (HibernateException e)
                {
                    log.Error(e, "Commit failed");
                    AfterTransactionCompletion(false);
                    commitFailed = true;
                    // Don't wrap HibernateExceptions
                    throw;
                }
                catch (Exception e)
                {
                    log.Error(e, "Commit failed");
                    AfterTransactionCompletion(false);
                    commitFailed = true;
                    throw new TransactionException("Commit failed with SQL exception", e);
                }
                finally
                {
                    CloseIfRequired();
                }
            }
        }
Example #6
0
        /// <summary>
        /// Commits the <see cref="ITransaction"/> by flushing the <see cref="ISession"/>
        /// and committing the <see cref="IDbTransaction"/>.
        /// </summary>
        /// <exception cref="TransactionException">
        /// Thrown if there is any exception while trying to call <c>Commit()</c> on
        /// the underlying <see cref="IDbTransaction"/>.
        /// </exception>
        public void Commit()
        {
            CheckNotDisposed();
            CheckBegun();

            log.Debug("commit");

            if (session.FlushMode != FlushMode.Never)
            {
                session.Flush();
            }

            session.BeforeTransactionCompletion(this);

            try
            {
                trans.Commit();
                committed = true;
                AfterTransactionCompletion(true);
                Dispose();
            }
            catch (HibernateException e)
            {
                log.Error("Commit failed", e);
                AfterTransactionCompletion(false);
                commitFailed = true;
                // Don't wrap HibernateExceptions
                throw;
            }
            catch (Exception e)
            {
                log.Error("Commit failed", e);
                AfterTransactionCompletion(false);
                commitFailed = true;
                throw new TransactionException("Commit failed with SQL exception", e);
            }
        }
Example #7
0
 public void BeforeTransactionCompletion(ITransaction tx)
 {
     _session.BeforeTransactionCompletion(tx);
 }
 public void beforeTransactionCompletion(ITransaction tx)
 {
     delegat.BeforeTransactionCompletion(tx);
 }