예제 #1
0
		public TransactionScope(TransactionMode mode) : base(SessionScopeType.Transactional)
		{
			this.mode = mode;

			ISessionScope previousScope = ScopeUtil.FindPreviousScope(this,  
				mode == TransactionMode.Inherits ? true : false );

			if (previousScope != null)
			{
				if (previousScope.ScopeType == SessionScopeType.Transactional)
				{
					parentTransactionScope = previousScope as TransactionScope;
				}
				else
				{
					// This is not a safe cast. Reconsider it
					parentSimpleScope = (AbstractScope) previousScope;

					foreach(ISession session in parentSimpleScope.GetSessions())
					{
						EnsureHasTransaction(session);
					}
				}
			}
		}
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TransactionScope"/> class.
        /// </summary>
        /// <param name="mode">Whatever to create a new transaction or inherits an existing one</param>
        /// <param name="isolationLevel">The transaction isolation level.</param>
        /// <param name="onDisposeBehavior">The on dispose behavior.</param>
        public TransactionScope(TransactionMode mode, IsolationLevel isolationLevel, OnDispose onDisposeBehavior)
            : base(FlushAction.Config, SessionScopeType.Transactional)
        {
            this.mode              = mode;
            this.isolationLevel    = isolationLevel;
            this.onDisposeBehavior = onDisposeBehavior;

            bool preferenceForTransactionScope = mode == TransactionMode.Inherits ? true : false;

            ISessionScope previousScope = ScopeUtil.FindPreviousScope(this, preferenceForTransactionScope);

            if (previousScope != null)
            {
                if (previousScope.ScopeType == SessionScopeType.Transactional)
                {
                    parentTransactionScope = previousScope as TransactionScope;
                }
                else
                {
                    // This is not a safe cast. Reconsider it
                    parentSimpleScope = (AbstractScope)previousScope;

                    foreach (ISession session in parentSimpleScope.GetSessions())
                    {
                        EnsureHasTransaction(session);
                    }
                }
            }
        }
예제 #3
0
		protected override void PerformDisposal(ICollection sessions)
		{
			if (mode == TransactionMode.Inherits && parentTransactionScope != null)
			{
				// In this case it's not up to this instance to perform the clean up
				return;
			}

			foreach (ITransaction transaction in _transactions.Values)
			{
				if (_rollbackOnly)
				{
					transaction.Rollback();
				}
				else
				{
					transaction.Commit();
				}
			}

			if (parentSimpleScope == null)
			{
				// No flush necessary, but we should close the session

				base.PerformDisposal(sessions, false, true);
			}
			else
			{
				if (_rollbackOnly)
				{
					// Cancel all pending changes 
					// (not sure whether this is a good idea, it should be scoped

					foreach( ISession session in parentSimpleScope.GetSessions() )
					{
						session.Clear();
					}
				}
			}

			RaiseOnCompleted();
		}
예제 #4
0
        /// <summary>
        /// Dispose of this scope
        /// </summary>
        /// <param name="sessions">The sessions.</param>
        protected override void PerformDisposal(ICollection <ISession> sessions)
        {
            if (!setForCommit && !rollbackOnly)             // Neither VoteCommit or VoteRollback were called
            {
                if (onDisposeBehavior == OnDispose.Rollback)
                {
                    VoteRollBack();
                }
            }

            if (mode == TransactionMode.Inherits && parentTransactionScope != null)
            {
                // In this case it's not up to this instance to perform the clean up
                return;
            }

            Exception transactionError = null;

            foreach (ITransaction transaction in transactions.Values)
            {
                try
                {
                    if (rollbackOnly)
                    {
                        transaction.Rollback();
                    }
                    else
                    {
                        transaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    transactionError = ex;

                    transaction.Dispose();
                }
            }

            if (parentSimpleScope == null)
            {
                // No flush necessary, but we should close the session

                PerformDisposal(sessions, false, true);
            }
            else
            {
                if (rollbackOnly)
                {
                    // Cancel all pending changes
                    // (not sure whether this is a good idea, it should be scoped

                    foreach (ISession session in parentSimpleScope.GetSessions())
                    {
                        session.Clear();
                    }
                }

                parentSimpleScope.ResetFlushMode();
            }

            RaiseOnCompleted();

            if (transactionError != null)
            {
                throw new NHibernate.TransactionException("An error occured when trying to dispose the transaction", transactionError);
            }
        }