Пример #1
0
 void IEnlistmentNotificationInternal.Prepare(
     IPromotedEnlistment preparingEnlistment
     )
 {
     _promotedEnlistment = preparingEnlistment;
     _twoPhaseNotifications.Prepare(PreparingEnlistment);
 }
        public void ExceptionUponCompletionRollback()
        {
            MockRepository mocks = new MockRepository();

            IEnlistmentNotification concurrencyConflictResource = mocks.StrictMock <IEnlistmentNotification>();

            concurrencyConflictResource.Prepare(null);
            LastCall.IgnoreArguments().Throw(new TransactionalConcurrencyException());

            mocks.ReplayAll();

            Transactional <SimpleObject> tso = new Transactional <SimpleObject>(new SimpleObject());

            tso.Value.TestInt    = 0;
            tso.Value.TestGuid   = Guid.Empty;
            tso.Value.TestString = null;

            Assert.Throws <TransactionalConcurrencyException>(() =>
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    tso.Value.TestInt    = 7;
                    tso.Value.TestGuid   = Guid.NewGuid();
                    tso.Value.TestString = "Testing";

                    Transaction.Current.EnlistVolatile(concurrencyConflictResource, EnlistmentOptions.None);

                    scope.Complete();
                }
            });

            Assert.AreEqual(0, tso.Value.TestInt);
            Assert.AreEqual(Guid.Empty, tso.Value.TestGuid);
            Assert.AreEqual(null, tso.Value.TestString);
        }
        /// <summary>
        /// Prepares the enlisted resource managers for committal.
        /// </summary>
        /// <returns><c>true</c> if all polled participants voted to commit;
        /// <c>false</c> otherwise.</returns>
        internal bool Prepare()
        {
            /*if (TransactionInformation.Status == TransactionStatus.Aborted)
             *      return false;
             *
             * if (TransactionInformation.Status != TransactionStatus.Active)
             *      throw new TransactionException("Cannot prepare transaction, as it is not active. Trasnaction Status: " + TransactionInformation.Status);
             */
            bool booVoteToCommit = true;

            PreparingEnlistment     lpeEnlistment   = null;
            IEnlistmentNotification entNotification = null;

            for (Int32 i = m_lstNotifications.Count - 1; i >= 0; i--)
            {
                entNotification = m_lstNotifications[i];
                lpeEnlistment   = new PreparingEnlistment();
                entNotification.Prepare(lpeEnlistment);
                if (lpeEnlistment.VoteToCommit.HasValue)
                {
                    booVoteToCommit &= lpeEnlistment.VoteToCommit.Value;
                    if (lpeEnlistment.DoneProcessing)
                    {
                        m_lstNotifications.RemoveAt(i);
                    }
                }
                else
                {
                    booVoteToCommit = false;
                    TransactionInformation.Status = TransactionStatus.InDoubt;
                }
            }
            if (TransactionInformation.Status == TransactionStatus.InDoubt)
            {
                NotifyInDoubt();
            }
            return(booVoteToCommit);
        }