Esempio n. 1
0
        /// <summary>
        /// Initiates a nested transaction.
        /// </summary>
        /// <returns>The nested <see cref="Transaction" />.</returns>
        /// <exception cref="TransactionException">Thrown if the transaction is not open.</exception>
        /// <exception cref="TransactionException">Thrown if the transaction is not on the top of the stack.</exception>
        public Transaction BeginTransaction()
        {
            using (TimedLock.Lock(syncLock))
            {
                if (!isOpen)
                {
                    throw new TransactionException(NotOpenMsg);
                }

                if (transBase.Count == 0 || !object.ReferenceEquals(this, transBase.Peek()))
                {
                    throw new TransactionException("This transaction is not on the top of the stack.");
                }

                return(transBase.BeginTransaction());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initiates a new base transaction.
        /// </summary>
        /// <returns>The new <see cref="Transaction" />.</returns>
        /// <exception cref="InvalidOperationException">Thrown if transaction manager has not been started.</exception>
        /// <remarks>
        /// <para>
        /// Note that this method changes its behavior based on whether
        /// <b>allowThreadSpanning</b> was passed as <c>true</c> or <c>false</c>
        /// to the constructor.  If thread spanning is allowed then this method
        /// always creates a new <see cref="BaseTransaction" /> and returns a
        /// new <see cref="Transaction" /> pushed onto it.
        /// </para>
        /// <para>
        /// If single-thread transaction mode is enabled, then this method
        /// will first look to see if there's already a <see cref="BaseTransaction" />
        /// for then current thread.  If there is one, then a new <see cref="Transaction" />
        /// will be pushed onto it and returned.  If there no base transaction associated
        /// with the thread then a new one will be created.
        /// </para>
        /// </remarks>
        public Transaction BeginTransaction()
        {
            BaseTransaction transBase;

            using (TimedLock.Lock(this))
            {
                if (!running)
                {
                    throw new TransactionException("Transaction manager has not started for [{0}].", resource.Name);
                }

                if (stopPending)
                {
                    throw new TransactionException("Transaction manager stop is pending for [{0}].", resource.Name);
                }

                if (threadSpanning)
                {
                    transBase = new BaseTransaction(Helper.NewGuid(), this);
                    transactions.Add(transBase.ID, transBase);

                    return(transBase.BeginTransaction());
                }
                else
                {
                    transBase = this.CurrentTransaction;
                    if (transBase != null)
                    {
                        return(transBase.BeginTransaction());
                    }

                    this.CurrentTransaction = transBase = new BaseTransaction(Helper.NewGuid(), this);
                    transactions.Add(transBase.ID, transBase);

                    return(transBase.BeginTransaction());
                }
            }
        }