使用transactionscrope时 如:
using (TransactionScope scop = new TransactionScope()) { OracleCommand cmd = new OracleCommand("insert into TMP_TEST values('" + DateTime.Now + "')", con, tran); con.Open(); var result2 = cmd.ExecuteNonQuery(); OracleCommand cmd3 = new OracleCommand("select COL1 from tmp_test", con); var t2 = cmd3.ExecuteOracleScalar(); Console.WriteLine("2受影响的行数:" + result + "||当前临时表数量:" + t2); OracleDataAdapter da = new OracleDataAdapter("select * from TMP_TEST", con); DataTable dt = new DataTable(); da.Fill(dt); Console.WriteLine("3受影响的行数:" + result2 + "||当前临时表数量:" + dt.Rows.Count); scop.Complete(); }
当启用高并发时,报错14450,并发实现为:
for (int i = 0; i < 100; i++) { new Thread(new ParameterizedThreadStart(new Class1().Func1)).Start(i); }
在及时提交时不会有问题,但是如果不提交则出现14450错误,
transactionscrope 提交|回滚 是在Dispose中执行的,
使用CommittableTransaction 如:
OracleConnection con = new OracleConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings["MB.MBERP"].ConnectionString; using (CommittableTransaction transaction = new CommittableTransaction()) { Transaction.Current = transaction; OracleCommand cmd = new OracleCommand("insert into TMP_TEST values('" + i + "')", con); con.Open(); // var result2 = 1; var result2 = cmd.ExecuteNonQuery(); OracleCommand cmd3 = new OracleCommand("select COL1 from tmp_test", con); var t2 = cmd3.ExecuteOracleScalar(); //Console.WriteLine("local: {0} ; distr: {1}", Transaction.Current.TransactionInformation.LocalIdentifier, Transaction.Current.TransactionInformation.DistributedIdentifier); Console.WriteLine(i + "受影响的行数:" + result2 + "||当前临时表数量:" + t2); long tick = DateTime.Now.Ticks; Thread.Sleep(new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)).Next(1000)); // transaction.Commit(); transaction.Rollback(); con.Close(); }
也是出现14450错误,commit则正常;
使用dbtrasnaction 如:
con.Open(); var tran = con.BeginTransaction(); OracleCommand cmd1 = new OracleCommand("insert into TMP_TEST values('" + DateTime.Now.Ticks + "')", con, tran); var result = cmd1.ExecuteNonQuery(); OracleCommand cmd2 = new OracleCommand("select COL1 from tmp_test", con, tran); var t = cmd2.ExecuteOracleScalar(); tran.Rollback(); con.Close(); Console.WriteLine(i + "受影响的行数:" + result + "||当前临时表数量:" + t);
则不会出问题