The Transaction Model
The transaction model is used to group and isolate sets of updates to database objects, in order to maintain data consistency.
All sets of updates must be applied and made visible as a group. Until the updates are ready, they should be invisible to other JADE sessions. If something goes wrong, all of the pending updates must be discarded.
In order to update objects, a JADE session must begin a transaction. The updates are held temporarily until the transaction is committed, at which time they get applied to the JADE database as permanent changes and made visible. Before that happens, other JADE sessions view the objects as they were without the uncommitted updates (this is termed transaction isolation).
If the full set of updates cannot be completed (for example, due to an error), the transaction is rolled back, which cancels the transaction and discards all of the pending updates in the set.
This concept implements the standard transaction ACID principles of atomicity (either all of the updates or none of the updates get applied), and isolation (the updates are not visible until applied).
With JADE, a transaction is typically started using the JoobContext BeginTransaction method. This method returns a System.Data.IDbTransaction JADE transaction object. The transaction object’s Commit method commits the transaction, which applies the updates to the database. Alternatively, the Rollback method cancels the transaction, discarding the uncommitted updates.
The transaction object must be disposed of at the end of the transaction. If Commit is not called before the transaction object is disposed of, the transaction will be rolled back.
You can use a using block to carry out a transaction and ensure that the transaction object is disposed of at the end of the block.
The following code fragment illustrates a successful transaction, where updates are permanently applied to the JADE database and made visible.
using (JoobContext context = new JoobContext()) { using (System.Data.IDbTransaction tran = context.BeginTransaction()) { //... update objects tran.Commit(); } }
The following code fragment illustrates a transaction that is rolled back, where updates are discarded and not applied.
using (System.Data.IDbTransaction tran = context.BeginTransaction()) { //... update objects tran.Rollback(); }