Prev | Next

TOC | Index

J2EETM Developer's Guide
Transactions


Chapter 6

Transactions


To emulate a business transaction, a program may need to perform several steps. A financial program, for example, might transfer funds from a checking account to a savings account with the steps listed in the following pseudo-code.

begin transaction 
   debit checking account 
   credit savings account
   update history log 
commit transaction
Either all three of these steps must complete, or none of them at all. Otherwise, data integrity is lost. Because the steps within a transaction are a unified whole, a transaction is often defined as an indivisible unit of work.

A transaction can end in two ways: with a commit or a rollback. When a transaction commits, the data modifications made by its statements are saved. If a statement within a transaction fails, the transaction rolls back, undoing the effects of all statements in the transaction. In the pseudo-code, for example, if a disk drive crashed during the credit step, the transaction rolls back and undoes the data modifications made by the debit statement. Although the transaction failed, data integrity is intact because the accounts still balance.

In the preceding pseudo-code, the begin and commit statments mark the boundaries of the transaction. When deploying an enterprise bean, you determine how the boundaries are set by specifying either container-managed or bean-managed transactions.



Prev | Next

TOC | Index


Copyright © 2000 Sun Microsystems, Inc. All rights reserved.