Java Reference
In-Depth Information
In addition to these three optimizations, during both the voting and commit phases, the
transaction manager may execute the calls concurrently to the resource managers. If there
are three databases and each database takes 10 ms to respond, then the entire voting phase
will take only 10 ms and not 30 ms as in the case where each resource manager was polled
sequentially.
6.2. Container-managed transactions
In a CMT, the container starts, commits, and rolls back a transaction on your behalf. Trans-
action boundaries in declarative transactions are always marked by the start and end of EJB
business methods. More precisely, the container starts a JTA transaction before a method
is invoked, invokes the method, and, depending on what happened during the method call,
either commits or rolls back the managed transaction. All you have to do is tell the contain-
er how to manage the transaction by using either annotations or deployment descriptors and
informing the transaction manager when the transaction needs to be rolled back. As men-
tioned previously, the container assumes that you'll be using CMT on all business methods.
This section dives into the nuts and bolts of CMT. You'll learn how to use the @Transac-
tionManagement and @TransactionAttribute annotations. In addition you'll
learn how to roll back a transaction using the EJBContext and how to handle application
exceptions.
6.2.1. Snag-it ordering using CMT
Some items on ActionBazaar have a “snag-it” ordering option. This option enables a user
to purchase an item on bid at a set price before anyone else bids on it. With this feature,
buyers and bidders alike don't have to wait for bidding to finish. As soon as the user clicks
the Snag-It button, the ActionBazaar application makes sure no bids have been placed on
the item, validates the buyer's credit card, charges the buyer, and removes the item from
bidding. It's important that this operation takes place within the context of a transaction.
Obviously, the buyer would be upset if they were billed for an item that never shipped, and
the seller would be upset if the item was shipped but no money collected.
To implement snag-it ordering, you'll add a placeSnagItOrder to the Order-Man-
agerBean , which is a stateless session bean. The code for this method is shown in the fol-
Search WWH ::




Custom Search