当前位置: 首页>>代码示例>>C#>>正文


C# Support.TransactionTemplate类代码示例

本文整理汇总了C#中Spring.Transaction.Support.TransactionTemplate的典型用法代码示例。如果您正苦于以下问题:C# TransactionTemplate类的具体用法?C# TransactionTemplate怎么用?C# TransactionTemplate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


TransactionTemplate类属于Spring.Transaction.Support命名空间,在下文中一共展示了TransactionTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReturnConnectionAfterCommit

        public void ReturnConnectionAfterCommit()
        {
            var txTemplate = new TransactionTemplate(new RabbitTemplateTestsTransactionManager());
            var mockConnectionFactory = new Mock<ConnectionFactory>();
            var mockConnection = new Mock<IConnection>();
            var mockChannel = new Mock<IModel>();

            mockConnectionFactory.Setup(m => m.CreateConnection()).Returns(mockConnection.Object);
            mockConnection.Setup(m => m.IsOpen).Returns(true);
            mockConnection.Setup(m => m.CreateModel()).Returns(mockChannel.Object);
            mockChannel.Setup(m => m.IsOpen).Returns(true);
            mockChannel.Setup(m => m.CreateBasicProperties()).Returns(() => new BasicProperties());

            var template = new RabbitTemplate(new CachingConnectionFactory(mockConnectionFactory.Object));
            template.ChannelTransacted = true;

            txTemplate.Execute(
                status =>
                {
                    template.ConvertAndSend("foo", "bar");
                    return null;
                });

            txTemplate.Execute(
                status =>
                {
                    template.ConvertAndSend("baz", "qux");
                    return null;
                });

            mockConnectionFactory.Verify(m => m.CreateConnection(), Times.Once());

            // ensure we used the same channel
            mockConnection.Verify(m => m.CreateModel(), Times.Once());
        }
开发者ID:yonglehou,项目名称:spring-net-amqp,代码行数:35,代码来源:RabbitTemplateTests.cs

示例2: ParticipatingTransactionWithCommit

        public void ParticipatingTransactionWithCommit()
        {
            IDbConnection connection = (IDbConnection) mocks.CreateMock(typeof (IDbConnection));
            ISessionFactory sessionFactory = (ISessionFactory) mocks.CreateMock(typeof (ISessionFactory));
            ISession session = (ISession) mocks.CreateMock(typeof (ISession));
            ITransaction transaction = (ITransaction) mocks.CreateMock(typeof (ITransaction));

            using (mocks.Ordered())
            {
                Expect.Call(sessionFactory.OpenSession()).Return(session);
                Expect.Call(session.Connection).Return(connection);
                Expect.Call(session.BeginTransaction(IsolationLevel.ReadCommitted)).Return(transaction);
                Expect.Call(session.IsOpen).Return(true);
                Expect.Call(session.FlushMode).Return(FlushMode.Auto);
                session.Flush();
                LastCall.On(session).Repeat.Once();
                transaction.Commit();
                LastCall.On(transaction).Repeat.Once();
                Expect.Call(session.Close()).Return(null);
            }

            mocks.ReplayAll();

            HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            IList list = new ArrayList();
            list.Add("test");

            object result = tt.Execute(new ParticipatingTransactionWithCommitTxCallback(sessionFactory, list));
            Assert.IsTrue(result == list);

            mocks.VerifyAll();
        }
开发者ID:serra,项目名称:spring-net,代码行数:35,代码来源:HibernateTransactionManagerTests.cs

示例3: ParticipatingTransactionWithCommit

        public void ParticipatingTransactionWithCommit()
        {
            IConnectionFactory connectionFactory = mocks.StrictMock<IConnectionFactory>();
            IConnection connection = mocks.StrictMock<IConnection>();
            ISession session = mocks.StrictMock<ISession>();

            using (mocks.Ordered())
            {
                SetupCommitExpectations(connection, connectionFactory, session);
            }

            mocks.ReplayAll();

            NmsTransactionManager tm = new NmsTransactionManager(connectionFactory);
            ITransactionStatus ts = tm.GetTransaction(new DefaultTransactionDefinition());
            NmsTemplate nt = new NmsTemplate(connectionFactory);
            nt.Execute(new AssertSessionCallback(session));

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.Execute(delegate(ITransactionStatus status)
                           {
                               nt.Execute(new AssertSessionCallback(session));
                               return null;
                           });

            tm.Commit(ts);

            mocks.VerifyAll();
        }
开发者ID:chadsowald,项目名称:spring-net,代码行数:29,代码来源:MessageTransactionManagerTests.cs

示例4: TxTemplate

        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;
                           });
        }
开发者ID:fuadm,项目名称:spring-net,代码行数:33,代码来源:NestedTxScopeTests.cs

示例5: TransactionCommit

        public void TransactionCommit()
        {
            ITransactionScopeAdapter txAdapter = (ITransactionScopeAdapter) mocks.CreateMock(typeof(ITransactionScopeAdapter));

            using (mocks.Ordered())
            {
                Expect.Call(txAdapter.IsExistingTransaction).Return(false);
                TransactionOptions txOptions = new TransactionOptions();
                txOptions.IsolationLevel = IsolationLevel.ReadCommitted;
                txAdapter.CreateTransactionScope(TransactionScopeOption.Required, txOptions, EnterpriseServicesInteropOption.None);

                Expect.Call(txAdapter.RollbackOnly).Return(false);
                txAdapter.Complete();
                txAdapter.Dispose();
            }
            mocks.ReplayAll();

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.Execute(status =>
                           {
                               Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive);
                               Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                               return null;
                           });

            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);   

            mocks.VerifyAll();


        }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:35,代码来源:TxScopeTransactionManagerTests.cs

示例6: Init

 public void Init()
 {
     var connectionFactory = new CachingConnectionFactory();
     this.template = new RabbitTemplate(connectionFactory);
     this.template.IsChannelTransacted = true;
     var transactionManager = new RabbitTransactionManager(connectionFactory);
     this.transactionTemplate = new TransactionTemplate(transactionManager);
     this.transactionTemplate.TransactionIsolationLevel = IsolationLevel.Unspecified;
 }
开发者ID:DonMcRae,项目名称:spring-net-amqp,代码行数:9,代码来源:RabbitTransactionManagerIntegrationTests.cs

示例7: NullResult

		public void NullResult()
		{
			MockTxnPlatformMgr mock = new MockTxnPlatformMgr();
			mock.SetExpectedGetTxnCallCount(1);
			mock.SetExpectedCommitCallCount(1);
			TransactionTemplate temp = new TransactionTemplate(mock);
			temp.AfterPropertiesSet();
			Assert.AreEqual( mock, temp.PlatformTransactionManager);
			Assert.IsNull( temp.Execute(new TransactionDelegate(DummyTransactionMethod) ) );
			mock.Verify();
		}
开发者ID:fuadm,项目名称:spring-net,代码行数:11,代码来源:TransactionTemplateTests.cs

示例8: PropagationRequiresNewWithExistingTransaction

        public void PropagationRequiresNewWithExistingTransaction()
        {
            ITransactionScopeAdapter txAdapter = (ITransactionScopeAdapter)mocks.CreateMock(typeof(ITransactionScopeAdapter));

            using (mocks.Ordered())
            {
                Expect.Call(txAdapter.IsExistingTransaction).Return(false);
                TransactionOptions txOptions = new TransactionOptions();
                txOptions.IsolationLevel = IsolationLevel.ReadCommitted;
                txAdapter.CreateTransactionScope(TransactionScopeOption.RequiresNew, txOptions, EnterpriseServicesInteropOption.None);

                //inner tx actions
                Expect.Call(txAdapter.IsExistingTransaction).Return(true);
                txAdapter.CreateTransactionScope(TransactionScopeOption.RequiresNew, txOptions, EnterpriseServicesInteropOption.None);
                txAdapter.Dispose();
                //end inner tx actions

                Expect.Call(txAdapter.RollbackOnly).Return(false);
                txAdapter.Complete();
                txAdapter.Dispose();

            }
            mocks.ReplayAll();

            TxScopeTransactionManager tm = new TxScopeTransactionManager(txAdapter);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);
            tt.PropagationBehavior = TransactionPropagation.RequiresNew;
            tt.Execute(delegate(ITransactionStatus status)
                           {
                               Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                               Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive, "Synchronization active");
                               Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                               Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);

                               tt.Execute(delegate(ITransactionStatus status2)
                                              {
                                                  Assert.IsTrue(TransactionSynchronizationManager.SynchronizationActive, "Synchronization active");
                                                  Assert.IsTrue(status2.IsNewTransaction, "Is new transaction");
                                                  Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                                                  Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
                                                  status2.SetRollbackOnly();
                                                  return null;
                                              });

                               Assert.IsTrue(status.IsNewTransaction, "Is new transaction");
                               Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
                               Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
                               return null;
                           });

            mocks.VerifyAll();
        }
开发者ID:smnbss,项目名称:spring-net,代码行数:54,代码来源:TxScopeTransactionManagerTests.cs

示例9: NullResult

        public void NullResult()
        {
            IPlatformTransactionManager mock = MockRepository.GenerateMock<IPlatformTransactionManager>();

            TransactionTemplate temp = new TransactionTemplate(mock);
            temp.AfterPropertiesSet();
            Assert.AreEqual(mock, temp.PlatformTransactionManager);
            Assert.IsNull(temp.Execute(new TransactionDelegate(DummyTransactionMethod)));

            mock.AssertWasCalled(x => x.GetTransaction(Arg<ITransactionDefinition>.Is.Anything), constraints => constraints.Repeat.Once());
            mock.AssertWasCalled(x => x.Commit(Arg<ITransactionStatus>.Is.Anything), constraints => constraints.Repeat.Once());
        }
开发者ID:Binodesk,项目名称:spring-net,代码行数:12,代码来源:TransactionTemplateTests.cs

示例10: ExecuteException

		public void ExecuteException()
		{
			MockTxnPlatformMgr mock = new MockTxnPlatformMgr();
			mock.SetExpectedGetTxnCallCount(1);
			mock.SetExpectedRollbackCallCount(1);
			TransactionTemplate temp = new TransactionTemplate(mock);
			try 
			{
				temp.Execute(new TransactionDelegate(DummyExceptionMethod));
				Assert.Fail("Should throw exception");
			} catch
			{
				
			}
			mock.Verify();
		}
开发者ID:fuadm,项目名称:spring-net,代码行数:16,代码来源:TransactionTemplateTests.cs

示例11: ExecuteException

        public void ExecuteException()
        {
            IPlatformTransactionManager mock = MockRepository.GenerateMock<IPlatformTransactionManager>();

            TransactionTemplate temp = new TransactionTemplate(mock);
            try
            {
                temp.Execute(new TransactionDelegate(DummyExceptionMethod));
            }
            catch
            {
            }

            mock.AssertWasCalled(x => x.GetTransaction(Arg<ITransactionDefinition>.Is.Anything), constraints => constraints.Repeat.Once());
            mock.AssertWasCalled(x => x.Rollback(Arg<ITransactionStatus>.Is.Anything), constraints => constraints.Repeat.Once());
        }
开发者ID:Binodesk,项目名称:spring-net,代码行数:16,代码来源:TransactionTemplateTests.cs

示例12: Commit

        public void Commit()
        {
            TxScopeTransactionManager tm = new TxScopeTransactionManager();
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            TransactionTemplate tt = new TransactionTemplate(tm);

            //tt.Name = "txName";

            Assert.AreEqual(TransactionSynchronizationState.Always, tm.TransactionSynchronization);
            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
            Assert.IsNull(TransactionSynchronizationManager.CurrentTransactionName);
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
            tt.Execute(CommitTxDelegate);
            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
            Assert.IsNull(TransactionSynchronizationManager.CurrentTransactionName);
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
        }
开发者ID:fgq841103,项目名称:spring-net,代码行数:18,代码来源:TxScopeTransactionManagerIntegrationTests.cs

示例13: ExistingTransactionWithPropagationNestedNotSupported

        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();
        }
开发者ID:serra,项目名称:spring-net,代码行数:44,代码来源:AdoPlatformTransactionManagerTests.cs

示例14: TransactionCommit

        public void TransactionCommit()
        {
            #region Mock Setup
            IServiceDomainAdapter txAdapter = (IServiceDomainAdapter) mocks.CreateMock(typeof (IServiceDomainAdapter));
            using (mocks.Ordered())
            {
                Expect.Call(txAdapter.IsInTransaction).Return(false);
                SimpleServiceConfig serviceConfig = new SimpleServiceConfig();
                ConfigureServiceConfig(serviceConfig, true);
                txAdapter.Enter(serviceConfig);

                //ProcessCommit - status.GlobalRollbackOnly check
                Expect.Call(txAdapter.MyTransactionVote).Return(TransactionVote.Commit);
                //DoCommit      - status.GlobalRollbackOnly check
                Expect.Call(txAdapter.MyTransactionVote).Return(TransactionVote.Commit);

                Expect.Call(txAdapter.IsInTransaction).Return(true);
                //DoCommit      - check to call SetComplete or SetAbort
                Expect.Call(txAdapter.MyTransactionVote).Return(TransactionVote.Commit);
                txAdapter.SetComplete();
                Expect.Call(txAdapter.Leave()).Return(TransactionStatus.Commited);

            }
            #endregion

            mocks.ReplayAll();

            ServiceDomainPlatformTransactionManager tm = new ServiceDomainPlatformTransactionManager(txAdapter);
            TransactionTemplate tt = new TransactionTemplate(tm);
            tm.TransactionSynchronization = TransactionSynchronizationState.Always;

            tt.Execute(new TransactionDelegate(TransactionCommitMethod));

            Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive);
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);

            mocks.VerifyAll();

        }
开发者ID:fgq841103,项目名称:spring-net,代码行数:39,代码来源:ServiceDomainTransactionManagerTests.cs

示例15: ParticipatingTransactionWithRollbackOnlyTxCallback

 public ParticipatingTransactionWithRollbackOnlyTxCallback(TransactionTemplate tt, ISessionFactory sf, IList list)
 {
     this.tt = tt;
     this.sf = sf;
     this.list = list;
 }
开发者ID:ouyangyl,项目名称:MySpringNet,代码行数:6,代码来源:HibernateTransactionManagerTests.cs


注:本文中的Spring.Transaction.Support.TransactionTemplate类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。