Some Common Programming Scenarios
In this section, we’ll look at some of the most common transaction scenarios and we’ll examine ways to leverage transactions effectively.
The subsection “Using a Session Façade” provides a concrete example of how to use CMTD effectively with an EJB component by minimizing the number of unnecessary transactions and increasing performance.
The subsection “Using Transaction Attributes with CMTD” illustrates an approach for utilizing transaction attributes with CMTD EJBs. This approach can help avoid resorting to BMTD in some situations.
The subsection “Distributed Transactions Involving a JMS Destination and Database” provides a detailed example of a distributed transaction using both JMS and JDBC resources.
Using a Session Façade
Consider the code fragment in Example 16-3, which uses a
Person
EJB component. Assume that
the EJB has been deployed using the Required
transaction attribute applied to
all of its methods. How many transactions does the user invoke in
total? The answer is four. Each EJB method, create()
, setWorkPhone()
, setHomePhone()
, and getPersonId()
, is invoked in a different
transaction. This is most likely not the intended behavior. This
code is not only very expensive but also may be incorrect. For
example, if there is some error setting the home phone number, the
client might expect the creation of a person bean instance to
fail.
Example 16-3. Direct usage
PersonHome aPersonHome = (PersonHome) aInitialContext.lookup("java:comp/env/ejb/Person"); ...
Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.