Exceptions and Transactions
Exceptions have a large impact on the outcome of transactions and must be discussed in detail so that bean developers understand the relationship between them.
Application Exceptions Versus System Exceptions
An application
exception is any exception that does not extend
java.lang.RuntimeException
or
java.rmi.RemoteException
. System exceptions are
java.lang.RuntimeException
and its subtypes,
including EJBException
.
Warning
An application exception must never extend either the
RuntimeException
, the
RemoteException
, or one of their subtypes.
Transactions are automatically rolled back if a system exception is thrown from an enterprise bean method. Transactions are not automatically rolled back if an application exception is thrown. If you remember these two rules, you will be well prepared to deal with exceptions and transactions in EJB.
The bookPassage()
method is a good illustration of
an application exception and how it is used. The following sections
show the code for the bookPassage()
method.
EJB 2.0: bookPassage( ) method
public TicketDO bookPassage(CreditCardDO card, double price) throws IncompleteConversationalState { if (customer == null || cruise == null || cabin == null) { throw new IncompleteConversationalState(); } try { ReservationHomeLocal resHome = (ReservationHomeLocal) jndiContext.lookup("java:comp/env/ejb/ReservationHomeLocal"); ReservationLocal reservation = resHome.create(customer, cruise, cabin, price); Object ref = jndiContext.lookup("java:comp/env/ejb/ProcessPaymentHomeRemote"); ...
Get Enterprise JavaBeans, 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.