Java and Transaction processing |
|
A typical enterprise application accesses
and stores information in one or more databases. This information
must be accurate, current and reliable as it is critical for
business operations. Data integrity would be lost if multiple
programs were allowed to simultaneously update the same information.
Also, it would be lost if a system that failed while processing
a business transaction were to leave the affected data only
partially updated. By preventing both of these scenarios,
software transactions ensure data integrity. Transactions
control the concurrent access of data by multiple programs.
In the event of a system failure, transactions make sure that
after recovery the data will be in a consistent state. Oracle9iAS
provides a full support for transactions. There are two types
of transactions:
- Container Managed Transactions
- Bean Managed Transactions
Container Managed Transactions
For an EJB with container-managed transactions, the container
sets the boundaries of the transactions. Container-managed
transactions can be used with any type of EJBs: session, entity
or message-driven beans. Container-managed transactions simplify
development because the EJB code does not explicitly mark
the transaction's boundaries. The code does not include statements
that begin and end the transaction.
Bean Managed Transactions
For an EJB with bean-managed transaction, the code in the
session or message-driven bean explicitly marks the boundaries
of the transaction. An entity bean may not have bean-managed
transactions; it must use container-managed transactions instead.
Although beans with container-managed transactions require
less coding, they have one limitation: When a method is executing,
it can be associated with either a single transaction or no
transaction at all. If this limitation will make coding your
bean difficult, you should consider using bean-managed transactions.
Demonstration
This sample (EJB
Transaction Processing) assumes 2 different schemas
for demonstrating transactions. The car information is contained
in car_dealer table in a different database. A transaction
is initiated before persisting the new reservation information
in the hotel schema and the car booking information in a different
schema. Only when both database operations succeed, the transaction
is committed. The following figure shows the architecture
of the EJB based implementation of the Hotel Reservation System.
Fig1: Architecture |