Transactions

The business object framework provides basic transaction handling. By default the business objects always connect and disconnect for each individual SQL statement. However, when Transactions are used the connection is cached and passed forward.

Transactions are easy to use:

busCustomer Customer = WebStoreFactory.GetbusCustomer();

Customer.BeginTransaction();

Customer.Load("1000");

wws_customersRow CustRow = Customer.GetTypedDataRow();

this.lblResult.Text = CustRow.Lastname;
CustRow.Lastname = "Updated Name";

Customer.Save();

Customer.New();
CustRow.GetTypedDataRow();
CustRow.Lastname = "Jimmy";
CustRow.Company = "Jimmini";

Customer.Save();

Customer.RollbackTransaction();

In this scenario a business object the Transaction is scope to the single business object and the entire set of commands operates under the single transaction context.

Be aware that although this works well this is not optimal for performance as you are locking any rows affected for the whole duration of the transaction.

Transactions spanning multiple business objects

By default a transaction is scoped to a specific business object and its connection property. In the above example only the Customer object is transacted. If we added code to use an Item object with in the scope of the transaction like this:

Customer.BeginTransaction();

Customer.Load("1000");
Customer.DataRow["LastName"] = "Updated Name";
Customer.Save();


busItem Item = WebStoreFactory.GetbusItem();
Item.New();
Item.DataRow["Sku"] = "NEWITEM";
Item.DataRow["Descript"] = "New Item Description";
Item.Save();

Customer.RollbackTransaction()

the transaction would have no effect, since the transaction is scoped to the Customer object. This is true even if you used the Item object in a method inside of the Customer object. If you want the Item object to participate in the Transaction you have to explicitly assign the transaction to the 'child' object:

IDbTransaction Transaction = Customer.BeginTransaction();

Item.SetTransaction(Transaction);

Now the above code will cause the rollback to rollback both the Customer and LineItem objects.

Transaction Caution

Your code is responsible for making sure that transactions are properly released. If you fail to call CommitTransaction() or RollbackTransaction() on an active connection you will hang your database. Therefore make sure you are extra careful in your use of try/catch/finally blocks that always clean up transactions.

You have access to the underlying Transaction object via:

wwBusiness.Data.Transaction

You can check this object for null and any of the properties if instantiated.


© West Wind Technologies, 1996-2018 • Updated: 12/17/03
Comment or report problem with topic