Java Reference
In-Depth Information
Finally, make use of the @TransactionAttribute . If a method always requires a
transaction and never starts it, mark it with MANDATORY . If a block of code must execute
in a transaction but should never trigger a rollback on the transaction of a caller, use
REQUIRES_NEW .
CMTs are great, but there are situations where you want more explicit control over a trans-
action. Let's take a look at BMTs.
6.3. Bean-managed transactions
The greatest strength of a CMT is also its greatest weakness. Using a CMT, you're limited
to having the transaction boundaries set at the beginning and end of business methods and
relying on the container to determine when a transaction starts, commits, or rolls back. A
BMT, on the other hand, allows you to specify these details programmatically, using se-
mantics similar to the JDBC transaction model. But even in this case, the container helps
you by actually creating the physical transaction, as well as taking care of a few low-
level details. With BMT, you must be much more aware of the underlying JTA transac-
tion API, primarily the javax.transaction.UserTransaction interface that we
mentioned earlier. Let's revisit the snag-it ordering code from listing 6.1 and reimplement
it using BMT. In the process, you'll learn how to use BMT.
6.3.1. Snag-it ordering using BMT
Listing 6.4 reimplements the code in listing 6.1 using BMT. The core business logic is the
same as in the CMT code listing. It checks to see if there are any bids, validates the card,
charges the card, and finally closes out the bid by shipping the item. Fundamentally the
code hasn't changed.
Search WWH ::




Custom Search