Esempio n. 1
0
        public void TxTemplate()
        {
            IDbProvider dbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
            dbProvider.ConnectionString = @"Data Source=MARKT60\SQL2005;Initial Catalog=CreditsAndDebits;User ID=springqa; Password=springqa";
            //IPlatformTransactionManager tm = new ServiceDomainPlatformTransactionManager();
            //IPlatformTransactionManager tm = new TxScopeTransactionManager();
            IPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            AdoTemplate adoTemplate = new AdoTemplate(dbProvider);

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.Required;
            tt.Execute(delegate(ITransactionStatus status)
                           {
                               if (System.Transactions.Transaction.Current != null) Console.WriteLine("tx 1 id = " + System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier);
                               Console.WriteLine("tx 1 'IsNewTransaction' = " + status.IsNewTransaction);
                               adoTemplate.ExecuteNonQuery(CommandType.Text, "insert into Credits (CreditAmount) VALUES (@amount)", "amount", DbType.Decimal, 0,444);
                               TransactionTemplate tt2 = new TransactionTemplate(tm);
                               tt2.PropagationBehavior = TransactionPropagation.RequiresNew;
                               
                               tt2.Execute(delegate(ITransactionStatus status2)
                                               {
                                                   if (System.Transactions.Transaction.Current != null) Console.WriteLine("tx 2 = " + System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier);
                                                   Console.WriteLine("tx 2 'IsNewTransaction' = " + status2.IsNewTransaction);
                                                   adoTemplate.ExecuteNonQuery(CommandType.Text, "insert into dbo.Debits (DebitAmount) VALUES (@amount)", "amount", DbType.Decimal, 0,555);
                                                   //throw new ArithmeticException("can't do the math.");
                                                   status2.SetRollbackOnly();
                                                   return null;
                                               });
                               
                               if (System.Transactions.Transaction.Current != null) Console.WriteLine("tx id1 = " + System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier);                               
                               return null;
                           });
        }
        public void ExistingTransactionWithPropagationNestedNotSupported()
        {
            #region Mock setup
            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));
            IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection));
            IDbTransaction transaction = (IDbTransaction)mocks.CreateMock(typeof(IDbTransaction));

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);

                transaction.Rollback();
                LastCall.On(transaction).Repeat.Once();
                connection.Dispose();
            }

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.Nested;

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            try
            {
                tt.Execute(new ExistingTransactionWithPropagationNestedCallback(dbProvider, tt));
                Assert.Fail("Should have thrown NestedTransactionNotSupportedException");
            } catch (NestedTransactionNotSupportedException)
            {
                // expected
            }

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            mocks.VerifyAll();
        }
        public void TransactionWithPropagationNested()
        {
            #region Mock setup
            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));
            IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection));
            IDbTransaction transaction = (IDbTransaction)mocks.CreateMock(typeof(IDbTransaction));

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);
                //standard tx timeout.
                transaction.Commit();
                LastCall.On(transaction).Repeat.Once();
                connection.Dispose();
            }

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.Nested;

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            tt.Execute(new TransactionDelegate(TransactionWithPropagationNestedMethod));

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            mocks.VerifyAll();
        }
        public void TransactionWithIsolation()
        {
            #region Mock setup
            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));
            IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection));
            IDbTransaction transaction = (IDbTransaction)mocks.CreateMock(typeof(IDbTransaction));

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(IsolationLevel.Serializable)).Return(transaction);
                //standard tx timeout.
                transaction.Commit();
                LastCall.On(transaction).Repeat.Once();
                connection.Dispose();
            }

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.RequiresNew;
            tt.TransactionIsolationLevel = IsolationLevel.Serializable;
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            tt.Execute(new TransactionCommitTxCallback(dbProvider));

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
        public void TransactionWithExceptionOnRollback()
        {
            #region Mock Setup

            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));
            IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection));
            IDbTransaction transaction = (IDbTransaction)mocks.CreateMock(typeof(IDbTransaction));

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);
                //standard tx timeout.
                transaction.Rollback();
                LastCall.On(transaction).Throw(new TestSqlException("Cannot commit", "314"));

                connection.Dispose();
            }

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            try
            {
                tt.Execute(new TransactionDelegate(TransactionWithExceptionOnRollbackMethod));
            }
            catch (TransactionSystemException)
            {
                //expected
            }

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
        public void TransactionWithExceptionOnBegin()
        {
            #region Mock setup
            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));

            // CreateConnection is called in AdoPlatformTransactionManager.DoBegin
            Expect.Call(dbProvider.CreateConnection()).Throw(new TestSqlException("Cannot begin", "314"));

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            try
            {
                tt.Execute(new TransactionDelegate(TransactionWithExceptionNoOp));
            } catch (CannotCreateTransactionException)
            {
                // expected
            }

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
        public void TransactionRollback()
        {
            #region Mock Setup

            IDbProvider dbProvider = (IDbProvider) mocks.CreateMock(typeof (IDbProvider));
            IDbConnection connection = (IDbConnection) mocks.CreateMock(typeof (IDbConnection));
            IDbTransaction transaction = (IDbTransaction) mocks.CreateMock(typeof (IDbTransaction));

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);
                //standard tx timeout.
                transaction.Rollback();
                LastCall.On(transaction).Repeat.Once();

                connection.Dispose();
            }

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            Exception ex = new ArgumentException("test exception");
            try
            {
                tt.Execute(new TransactionRollbackTxCallback(dbProvider, ex));
                Assert.Fail("Should have thrown exception");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(ex, e);
            }

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            mocks.VerifyAll();
        }
        public void PropagationNeverWithExistingTransaction()
        {
            #region Mock Setup
            IDbProvider dbProvider = mocks.StrictMock<IDbProvider>();
            IDbConnection connection = mocks.StrictMock<IDbConnection>();
            IDbTransaction transaction = mocks.StrictMock<IDbTransaction>();

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(DefaultIsolationLevel)).Return(transaction);
                //standard tx timeout.
                transaction.Rollback();
                LastCall.On(transaction).Repeat.Once();

                connection.Dispose();
            }
            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.RequiresNew;
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            try
            {
                tt.Execute(new PropagationNeverWithExistingTransactionCallback(tt));
                Assert.Fail("Should have thrown IllegalTransactionStateException");
            } catch (IllegalTransactionStateException)
            {
                //expected.
            }
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
        public void PropagationRequiresNewWithExistingTransactionAndUnrelatedDataSource()
        {
            IDbProvider dbProvider = (IDbProvider) mocks.CreateMock(typeof (IDbProvider));
            IDbConnection connection = (IDbConnection) mocks.CreateMock(typeof (IDbConnection));
            IDbTransaction transaction = (IDbTransaction) mocks.CreateMock(typeof (IDbTransaction));

            Expect.Call(dbProvider.CreateConnection()).Return(connection);
            connection.Open();
            LastCall.On(connection).Repeat.Once();
            Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);
            transaction.Commit();
            LastCall.On(transaction).Repeat.Once();
            connection.Dispose();

            IDbProvider dbProvider2 = (IDbProvider) mocks.CreateMock(typeof (IDbProvider));
            IDbConnection connection2 = (IDbConnection) mocks.CreateMock(typeof (IDbConnection));
            IDbTransaction transaction2 = (IDbTransaction) mocks.CreateMock(typeof (IDbTransaction));

            Expect.Call(dbProvider2.CreateConnection()).Return(connection2);
            connection2.Open();
            LastCall.On(connection2).Repeat.Once();
            Expect.Call(connection2.BeginTransaction(_defaultIsolationLevel)).Return(transaction2);
            transaction2.Rollback();
            LastCall.On(transaction2).Repeat.Once();
            connection2.Dispose();

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.RequiresNew;

            AdoPlatformTransactionManager tm2 = new AdoPlatformTransactionManager(dbProvider2);
            tm2.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt2 = new TransactionTemplate(tm2);
            tt2.PropagationBehavior = TransactionPropagation.RequiresNew;

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider2), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            tt.Execute(new PropagationRequiresNewWithExistingTransactionCallback(tt2, dbProvider));

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider2), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
        public void ParticipatingTransactionWithRollbackOnly()
        {
            IDbProvider dbProvider = (IDbProvider) mocks.CreateMock(typeof (IDbProvider));
            IDbConnection connection = (IDbConnection) mocks.CreateMock(typeof (IDbConnection));
            IDbTransaction transaction = (IDbTransaction) mocks.CreateMock(typeof (IDbTransaction));

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);
                //standard tx timeout.
                transaction.Rollback();
                LastCall.On(transaction).Repeat.Once();
                connection.Dispose();
            }
            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            Assert.IsFalse(TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition());
            TestTransactionSynchronization synch =
                new TestTransactionSynchronization(dbProvider, TransactionSynchronizationStatus.Rolledback);
            TransactionSynchronizationManager.RegisterSynchronization(synch);

            bool outerTransactionBoundaryReached = false;
            try
            {
                Assert.IsTrue(ts.IsNewTransaction);
                TransactionTemplate tt = new TransactionTemplate(tm);
                TransactionTemplate tt2 = new TransactionTemplate(tm);
                tt.Execute(new ParticipatingTxWithRollbackOnlyTxCallback(tt2, dbProvider));
                outerTransactionBoundaryReached = true;
                tm.Commit(ts);
                Assert.Fail("Should have thrown UnexpectedRollbackException");
            }
            catch (UnexpectedRollbackException)
            {
                // expected
                if (!outerTransactionBoundaryReached)
                {
                    tm.Rollback(ts);
                }
                Assert.IsTrue(outerTransactionBoundaryReached);
            }

            Assert.IsFalse(TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsFalse(synch.beforeCommitCalled);
            Assert.IsTrue(synch.beforeCompletionCalled);
            Assert.IsFalse(synch.afterCommitCalled);
            Assert.IsTrue(synch.afterCompletionCalled);

            mocks.VerifyAll();
        }
        private double UseAdoTemplateApiOneTx(int numIterations)
        {
            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(adoTemplate.DbProvider);

            TransactionTemplate tt = new TransactionTemplate(tm);
            double timeElapsed = 0;
            tt.Execute(status =>
                           {
                               start = DateTime.Now;
                               for (int i = 0; i < numIterations; i++)
                               {
                                   adoTemplate.ExecuteNonQuery(CommandType.Text, cmdText);
                               }
                               stop = DateTime.Now;
                               timeElapsed = Elapsed;
                               PrintTest("AdoTemplateApi", numIterations, timeElapsed);
                               return null;
                           });
            return timeElapsed;
        }
        public void TransactionWithPropagationNever()
        {
            IDbProvider dbProvider = mocks.StrictMock<IDbProvider>();

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.Never;

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            tt.Execute(new TransactionWithPropagationNotSupportedCallback(dbProvider));

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            mocks.VerifyAll();
        }
        public void TransactionWithExceptionOnCommitAndRollbackOnCommitFailure()
        {
            #region Mock Setup
            IDbProvider dbProvider = mocks.StrictMock<IDbProvider>();
            IDbConnection connection = mocks.StrictMock<IDbConnection>();
            IDbTransaction transaction = mocks.StrictMock<IDbTransaction>();

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();
                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(DefaultIsolationLevel)).Return(transaction);
                transaction.Commit();
                LastCall.On(transaction).Throw(new TestSqlException("Cannot commit", "314"));

                transaction.Rollback();
                LastCall.On(transaction).Repeat.Once();

                connection.Dispose();
            }
            #endregion
            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            tm.RollbackOnCommitFailure = true;
            TransactionTemplate tt = new TransactionTemplate(tm);

            try
            {
                tt.Execute(new TransactionDelegate(TransactionWithExceptionNoOp));
            }
            catch (TransactionSystemException)
            {
                //expected
            }

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
        public void PropagationRequiresNewWithExistingConnection()
        {
            #region Mock Setup

            IDbProvider dbProvider = mocks.StrictMock<IDbProvider>();
            IDbConnection connection = mocks.StrictMock<IDbConnection>();
            
            Expect.Call(dbProvider.CreateConnection()).Return(connection);
            connection.Open();
            LastCall.On(connection).Repeat.Once();
            connection.Dispose();

            IDbConnection connection2 = mocks.StrictMock<IDbConnection>();
            IDbTransaction transaction2 = mocks.StrictMock<IDbTransaction>();

            Expect.Call(dbProvider.CreateConnection()).Return(connection2);
            connection2.Open();
            LastCall.On(connection2).Repeat.Once();
            Expect.Call(connection2.BeginTransaction(DefaultIsolationLevel)).Return(transaction2);
            transaction2.Commit();
            LastCall.On(transaction2).Repeat.Once();
            connection2.Dispose();

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.Supports;
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            tt.Execute(new PropagationRequiresNewWithExistingConnectionCallback(tt, connection, connection2, dbProvider));


            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
        public void TransactionWithPropagationSupports()
        {
            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.Supports;

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            tt.Execute(new TransactionWithPropagationSupportsCallback(dbProvider));

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            mocks.VerifyAll();
        }
        public void PropagationRequiresNewWithExistingTransactionAndUnrelatedFailingDataSource()
        {
            #region Mock Setup
            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));
            IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection));
            IDbTransaction transaction = (IDbTransaction)mocks.CreateMock(typeof(IDbTransaction));

            Expect.Call(dbProvider.CreateConnection()).Return(connection);
            connection.Open();
            LastCall.On(connection).Repeat.Once();
            Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);
            transaction.Rollback();
            LastCall.On(transaction).Repeat.Once();
            connection.Dispose();

            IDbProvider dbProvider2 = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));
            IDbConnection connection2 = (IDbConnection)mocks.CreateMock(typeof(IDbConnection));

            Expect.Call(dbProvider2.CreateConnection()).Return(connection2);
            connection2.Open();
            Exception failure = new Exception("can't open connection");
            LastCall.On(connection2).Throw(failure);

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.RequiresNew;

            AdoPlatformTransactionManager tm2 = new AdoPlatformTransactionManager(dbProvider2);
            tm2.TransactionSynchronization = TransactionSynchronizationState.Never;
            TransactionTemplate tt2 = new TransactionTemplate(tm2);
            tt2.PropagationBehavior = TransactionPropagation.RequiresNew;

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider2), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active");

            try
            {
                tt.Execute(
                    new PropagationRequiresNewWithExistingTransactionAndUnrelatedFailingDataSourceCallback(tt2));
                Assert.Fail("Should have thrown CannotCreateTransactionException");
            } catch(CannotCreateTransactionException ex)
            {
                Assert.AreSame(failure, ex.InnerException);
            }

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");
            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider2), "Hasn't thread db provider");
            mocks.VerifyAll();
        }
        private void DoTransactionWithTimeout(int timeout)
        {
            #region Mock setup

            IDbProvider dbProvider = (IDbProvider)mocks.CreateMock(typeof(IDbProvider));
            IDbConnection connection = (IDbConnection)mocks.CreateMock(typeof(IDbConnection));
            IDbTransaction transaction = (IDbTransaction)mocks.CreateMock(typeof(IDbTransaction));
            IDbCommand command = (IDbCommand) mocks.CreateMock(typeof (IDbCommand));

            using (mocks.Ordered())
            {
                Expect.Call(dbProvider.CreateConnection()).Return(connection);
                connection.Open();

                LastCall.On(connection).Repeat.Once();
                Expect.Call(connection.BeginTransaction(_defaultIsolationLevel)).Return(transaction);
                Expect.Call(connection.CreateCommand()).Return(command);
                command.CommandText = "some SQL statement";
                LastCall.On(command).Repeat.Once();
                if (timeout > 1)
                {
                    command.CommandTimeout = (timeout - 1);
                    transaction.Commit();
                } else
                {
                    transaction.Rollback();
                }
                LastCall.On(transaction).Repeat.Once();
                connection.Dispose();
            }

            #endregion

            mocks.ReplayAll();

            AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(dbProvider);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.TransactionTimeout = timeout;

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            try
            {
                tt.Execute(new TransactionWithTimeoutCallback(dbProvider));
                if (timeout <= 1)
                {
                    Assert.Fail("Should have thrown TransactionTimedOutException");
                }
            } catch (TransactionTimedOutException)
            {
                if (timeout <=1 )
                {
                    //expected
                } else
                {
                    throw;
                }
            }

            Assert.IsTrue(!TransactionSynchronizationManager.HasResource(dbProvider), "Hasn't thread db provider");

            mocks.VerifyAll();
        }
Esempio n. 18
0
 /// <summary>
 /// TBD
 /// </summary>
 public static IPlatformTransaction CreateTransaction(IDbProvider dbProvider, ITransactionDefinition txDefinition)
 {
     AdoPlatformTransactionManager txMgr = new AdoPlatformTransactionManager(dbProvider);
     ITransactionStatus txStatus = txMgr.GetTransaction(txDefinition);
     return new PlatformTransactionHolder(txStatus, txMgr);
 }