public override void Run(Object context) { GFXDTransaction transx = Connection.BeginTransaction(); DbController dbc = new DbController(Connection); try { Connection.AutoCommit = false; Connection.BeginGFXDTransaction(); Order order = (Order)ObjectFactory.Create(ObjectType.Order); order.Customer = dbc.GetRandomCustomer(); dbc.AddOrder(order); IList <Product> products = dbc.GetRandomProducts(5); foreach (Product product in products) { OrderDetail ordDetail = new OrderDetail(); ordDetail.OrderId = order.OrderId; ordDetail.ProductId = product.ProductId; ordDetail.UnitPrice = product.RetailPrice; ordDetail.Quantity = DbHelper.GetRandomNumber(1); ordDetail.Discount = 0; dbc.AddOrderDetail(ordDetail); product.UnitsInStock -= ordDetail.Quantity; dbc.UpdateProduct(product); order.SubTotal += (ordDetail.UnitPrice * ordDetail.Quantity); } dbc.UpdateOrder(order); Connection.Commit(); } catch (Exception e) { Connection.Rollback(); Fail(e); } finally { base.Run(context); } }
public override void Run(Object context) { GFXDTransaction tx = null; String tableName = null; int numRows = 10; try { tx = Connection.BeginTransaction(IsolationLevel.ReadCommitted); tableName = DbRandom.BuildRandomTable(numRows); Command.CommandText = "SELECT * FROM " + tableName; DataTable dt = new DataTable(); DataAdapter.Fill(dt); if (dt.Rows.Count != 10) { Fail("Transaction failed to create table"); return; } DbHelper.ExecuteNonQueryStatement(Connection, "DELETE FROM " + tableName); tx.Commit(); dt.Clear(); DataAdapter.Fill(dt); if (dt.Rows.Count != 0) { Fail("Transaction failed to delete all table records"); } } catch (Exception e) { Fail(e); tx.Rollback(); } finally { DbHelper.DropTable(tableName); base.Run(context); } }
public void TransactionTest() { // Open a new connection to the network server running on localhost string host = "localhost"; int port = s_clientPort; string connStr = string.Format("server={0}:{1}", host, port); using (GFXDClientConnection conn = new GFXDClientConnection(connStr)) { conn.Open(); // create a table GFXDCommand cmd = new GFXDCommand("create table t1 (id int primary" + " key, addr varchar(20))", conn); cmd.ExecuteNonQuery(); try { GFXDTransaction tran = conn.BeginTransaction(IsolationLevel .ReadCommitted); cmd.Transaction = tran; // insert into the table using positional parameters cmd = new GFXDCommand("insert into t1 (id, addr) values (?, ?)", conn); cmd.Prepare(); for (int i = 0; i < s_numInserts; i++) { cmd.Parameters.Clear(); cmd.Parameters.Add(i); cmd.Parameters.Add("addr" + i); cmd.ExecuteNonQuery(); } tran.Commit(); // check the inserts VerifyInserts(conn, s_numInserts); // fire some updates and if any unsuccessful then rollback the transaction cmd.CommandText = "update t1 set addr = ? where id = ?"; tran = conn.BeginTransaction(IsolationLevel.ReadCommitted); cmd.Transaction = tran; bool success = true; for (int i = 100; i < 200; i++) { cmd.Parameters.Clear(); cmd.Parameters.Add("address" + i); cmd.Parameters.Add(i); if (cmd.ExecuteNonQuery() != 1) { // update failed; rolling back the entire transaction success = false; tran.Rollback(); break; } } // command for verification string verifyText = "select * from t1 where id >= 100 and id < 200"; if (success) { // verify the updates in transactional data VerifyInserts(conn, verifyText, 100, 199, "address"); // all succeeded; commit the transaction tran.Commit(); // verify the updates after commit VerifyInserts(conn, verifyText, 100, 199, "address"); } else { // verify no updates VerifyInserts(conn, verifyText, 100, 199, "addr"); } } finally { // drop the table cmd = new GFXDCommand("drop table t1", conn); cmd.ExecuteNonQuery(); conn.Close(); } } }