On Returning Serialized Data Objects from Business Methods


The getQuote business method returns to the client a serialized Quote object. In Listing 6-6 and in all of the CMT bean examples presented here, the serialized persistent object is filtered through the respond method in the com.ysoft.jdo.book.common.ejb.EJBHelper class. Before explaining what the respond method does, let's examine why it is needed.

In all CMT beans, the EJB container determines whether to end the transaction before it returns the response to the client. As a result, by the time serialization occurs, the transaction may have ended. Even in a BMT bean, if the business method ends the transaction, the transaction ends before the response is serialized to the client. As a result, the bean programmer must decide whether to serialize to the client an object graph that is consistent with the transactional context or to serialize an object graph that is obtained after the transaction ends. In the second case, the transaction's NontransactionalRead property must be turned on to allow JDO's transparent support for serialization to access the persistent objects.

Making sure that the persistence manager can support serialization of the business method's response becomes more difficult when the bean's code closes the persistence manager at the end of the business method. As mentioned earlier, a CMT bean closes the persistence manager because it cannot reuse it and because closing it allows the persistence manager and its resources to be recycled. Although the JDO implementation delays the effects of closing until the container-managed transaction ends, thereafter the persistence manager is actually closed. Once the persistence manager is actually closed, the behavior of managed objects becomes undefined. As a result, the behavior of the managed objects becomes undefined before the container uses them to serialize the return value.

The respond method in the EJBHelper class addresses both the transactional issue and the closed persistence manager issue by creating a graph of unmanaged objects for the response before the transaction ends and the persistence manager closes. It is this graph of unmanaged objects that the container serializes back to the client. The respond method uses CPU cycles to do its work and may generate additional objects that are eventually garbage collected. In addition, the use of the respond method guarantees that the method returns its response by value rather than by reference. This fact may have an impact on the decision whether to expose the business method in a local interface or call the business method from another method within the bean.

When the bean uses BMT, it is not necessary to close the persistence manager at the end of the business method. As a result, turning on the NontransactionalRead property to provide a nontransactional response after the transaction has ended may be preferable to calling the respond method to generate a transactional response.




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