O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required