コード例 #1
0
        public void IfOneWorks_AndOneFails_FirstShouldRollback()
        {
            var newGuid = Guid.NewGuid();

            for (int i = 0; i < 5; i++)
            {
                using (var tx = new CommittableTransaction(new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadCommitted
                }))
                    using (new TxScope(tx, AmbientTransactionOption.Enabled, NullLogger.Instance))             // set ambient also!
                    {
                        var first = new AnotherEnlistment(false);
                        System.Transactions.Transaction.Current.EnlistDurable(newGuid, first, EnlistmentOptions.None);
                        var second = new AnotherEnlistment(true);
                        System.Transactions.Transaction.Current.EnlistDurable(newGuid, second, EnlistmentOptions.None);

                        try
                        {
                            tx.Commit();
                            Assert.Fail("we have a failing resource");
                        }
                        catch (TransactionAbortedException)
                        {
                            Assert.That(second.RolledBack, Is.False);

                            first.HasRolledBack.WaitOne();
                            Assert.That(first.RolledBack, Is.True);
                        }
                    }
            }
        }
コード例 #2
0
		public void IfOneWorks_AndOneFails_FirstShouldRollback()
		{
			var newGuid = Guid.NewGuid();
			for (int i = 0; i < 5; i++)
			{
				using (var tx = new CommittableTransaction(new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted }))
				using (new TxScope(tx, NullLogger.Instance)) // set ambient also!
				{
					var first = new AnotherEnlistment(false);
					System.Transactions.Transaction.Current.EnlistDurable(newGuid, first, EnlistmentOptions.None);
					var second = new AnotherEnlistment(true);
					System.Transactions.Transaction.Current.EnlistDurable(newGuid, second, EnlistmentOptions.None);

					try
					{
						tx.Commit();
						Assert.Fail("we have a failing resource");
					}
					catch (TransactionAbortedException)
					{
						Assert.That(second.RolledBack, Is.False);

						first.HasRolledBack.WaitOne();
						Assert.That(first.RolledBack, Is.True);
					}
				}
			}
			
		}