/// <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)); }
public void RollbackTransaction() { if (_AdoPlatformTransactionManager == null) { throw new Exception("Currently there is no open transactions, cannot be rolled back"); } _AdoPlatformTransactionManager.Rollback(_ITransactionStatus); _AdoPlatformTransactionManager = null; IsInTrans = false; }
public void CommitTransaction() { if (_AdoPlatformTransactionManager == null) { throw new Exception("Currently there is no open transactions, cannot be submitted"); } _AdoPlatformTransactionManager.Commit(_ITransactionStatus); _AdoPlatformTransactionManager = null; IsInTrans = false; }
public void BeginTransaction() { if (_AdoPlatformTransactionManager != null) { CommitTransaction();//当已经开启过事务,默认提交该事务。 } IDbProvider.ConnectionString = this.ConnnectString; _AdoPlatformTransactionManager = new AdoPlatformTransactionManager(IDbProvider); TransactionTemplate tt = new TransactionTemplate(_AdoPlatformTransactionManager); _AdoPlatformTransactionManager.TransactionSynchronization = TransactionSynchronizationState.Never; tt.TransactionIsolationLevel = IsolationLevel.ReadCommitted; DefaultTransactionDefinition df = new DefaultTransactionDefinition(); df.PropagationBehavior = TransactionPropagation.Required; _ITransactionStatus = _AdoPlatformTransactionManager.GetTransaction(df); IsInTrans = true; }
public void TxTemplate() { IDbProvider dbProvider = DbProviderFactory.GetDbProvider("System.Data.SqlClient"); dbProvider.ConnectionString = @"Data Source=SPRINGQA;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); }); }
private double UseAdoTemplateApiOneTx(int numIterations) { AdoPlatformTransactionManager tm = new AdoPlatformTransactionManager(adoTemplate.DbProvider); TransactionTemplate tt = new TransactionTemplate(tm); double timeElapsed = 0; tt.Execute(delegate(ITransactionStatus 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); }