Chapter 11: Using JDO in Enterprise JavaBeans


Overview

Contrary to a common misconception, JDO is not a replacement for Enterprise JavaBeans (EJBs). The EJB specification is primarily concerned with sharing access to remote objects and controlling distributed transactions, while JDO is primarily concerned with an object's relationship to persistent storage. Nevertheless, EJB containers and JDO provide overlapping services for managing objects. Both provide transactional semantics for objects, and both entity beans and persistent objects have mechanisms that tie object state to persistent storage. Although the overlap in services fosters the misconception that JDO and EJBs are in competition, in fact, JDO can be used with EJBs, and to some extent, it eases their development.

There are three basic types of EJBs: session beans, entity beans, and message-driven beans. Session beans are the oldest type and have been used extensively in real-world development. There are two common patterns for building session beans that interact with a datastore. In the first pattern, which might be called the Session-Only pattern, the session bean talks directly to the datastore. The session bean might be stateless or stateful. Likewise, it might use container-managed transactions (CMT) or bean-managed transactions (BMT). In the past, the session bean used a datastore API such as JDBC to access persistent state. Meanwhile, the bean's business methods serve the client by returning and accepting serialized objects.

JDO plugs neatly into session beans that use the Session-Only pattern by replacing the code that calls the datastore API. As a general rule, the amount of code needed to use JDO is quite a bit less than the code it replaces. It is also more powerful, providing caching of persistent state, transparent navigation of the object model, and so forth. The EJB client, however, is unaware of the change. The client continues to pass and receive serialized objects that are not persistent.

The Session Facade pattern is the second pattern used for session beans. In this pattern, the session bean often uses entity beans to retrieve and store persistent state and generally uses CMT. It continues to interact with the client by passing and receiving serialized objects that are not persistent. In this pattern, introducing JDO does not change the client or the session bean. Instead, the change occurs in the entity beans. Alternatively, because JDO simplifies the access to persistent state, it may be reasonable to replace the use of entity beans in the Session Facade pattern with the use of JDO in the Session-Only pattern.

Entity beans may use container-managed persistence (CMP) or bean-managed persistence (BMP). When they use CMP, no persistence-related code is included inside the application code of the entity bean, and as a result, there is no reason to use JDO. It is possible that the container itself will use JDO to implement CMP, but that is a different story altogether. When the entity beans use BMP, the application developer can replace the code that calls the datastore API code with the simpler code that calls JDO.

The JDO Learning Tools use JBoss, an open source product, as the EJB container. After describing the steps required to set up JBoss and run the builds, this chapter examines the final iteration of the rental application. In this case, it is an enterprise application that uses an EJB to implement the reservation service. The rental enterprise application uses an HTML client that is very much like the one presented in Chapter 10. To the user it appears identical, but the controller servlet is somewhat different.

After examining the rental enterprise application, this chapter goes on to explain how to build the quote server application that Chapter 6 discusses. The quote server is a simple service that is implemented in the following EJB types:

  • Stateless CMT session bean

  • Stateless BMT session bean

  • Stateful CMT session bean

  • Stateful BMT session bean

  • BMP entity bean

Although the quote server example is simple, it shows JDO at work in many types of EJBs. One console client exercises all incantations of the quote server EJB.

In particular, the QuoteServerClient directly uses the entity bean version of the quote server. Having the user interface directly use an entity bean is generally a bad design for an application. The indictment, however, applies to the client's design and does not implicate the design of the entity bean. The quote server entity bean provides a good example of the use of JDO within a BMP entity bean. Although flawed, the QuoteServerClient demonstrates and tests the entity bean, as well as the other EJB types.




Using and Understanding Java Data Objects
Using and Understanding Java Data Objects
ISBN: 1590590430
EAN: 2147483647
Year: 2005
Pages: 156
Authors: David Ezzio

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net