예제 #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 SetUp()
        {
            IKernel kernel = new StandardKernel(new NinjectSettings { LoadExtensions = false });
            kernel.Load(new LinFuModule());
            kernel.Load(new TransactionsModule());
            

            // TODO externalise the test database connection information
            IDbProvider dbProvider = DbProviderFactory.GetDbProvider("MySql.Data.MySqlClient");
            dbProvider.ConnectionString = "SERVER=localhost; DATABASE=chameleon; UID=root; PASSWORD=;";

            kernel.Bind<IDbProvider>().ToConstant(dbProvider).InSingletonScope();

            _adoTemplate = kernel.Get<AdoTemplate>();
            _service = kernel.Get<Service>();

            _adoTemplate.ExecuteNonQuery(CommandType.Text,
                                         "CREATE TABLE IF NOT EXISTS TransactionIntegrationTest ("
                                         + "   id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, "
                                         + "   `name` VARCHAR(100)" + ");"
                                         + "TRUNCATE TABLE TransactionIntegrationTest;");
        }
예제 #3
0
        private static void executeUpdate(AdoTemplate jdbcTemplate, String sql, Object[][] args, string tableName)
        {
            jdbcTemplate.ExecuteNonQuery(CommandType.Text, String.Format("SET IDENTITY_INSERT {0} ON", tableName));

            foreach(Object[] arg in args)
                jdbcTemplate.ExecuteNonQuery(CommandType.Text, String.Format(sql, arg.Select(a => a ?? "NULL").ToArray()).Replace("'NULL'", "NULL"));

            jdbcTemplate.ExecuteNonQuery(CommandType.Text, String.Format("SET IDENTITY_INSERT {0} OFF", tableName));
        }
예제 #4
0
        private void CleanDb(IApplicationContext context)
        {
            IDbProvider dbProvider = (IDbProvider)context["DebitDbProvider"];
            adoTemplateDebit = new AdoTemplate(dbProvider);
            adoTemplateDebit.ExecuteNonQuery(CommandType.Text, "truncate table Debits");

            dbProvider = (IDbProvider)context["CreditDbProvider"];
            adoTemplateCredit = new AdoTemplate(dbProvider);
            adoTemplateCredit.ExecuteNonQuery(CommandType.Text, "truncate table Credits");

        }