EJB 1.1: Exceptions and Transactions

Application Exceptions Versus System Exceptions

In EJB 1.1, an application exception is any exception that does not extend java.lang.RuntimeException or the java.rmi.RemoteException. System exceptions are java.lang.RuntimeException and java.rmi.RemoteException types and subtypes, including EJBException.

Transactions are automatically rolled back if a system exception is thrown from a 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 1.1.

The bookPassage() method provides a good illustration of an application exception and how it’s used. The following code shows the bookPassage() method with the relevant exception handling in bold:

public Ticket bookPassage(CreditCard card, double price)
    throws IncompleteConversationalState {

    if (customer == null || cruise == null || cabin == null) {
                        throw new IncompleteConversationalState();
                   }
    try {
        ReservationHome resHome = (ReservationHome)
            getHome("ReservationHome",ReservationHome.class);
        Reservation reservation =
        resHome.create(customer, cruise, cabin, price);
        ProcessPaymentHome ppHome = (ProcessPaymentHome)
            getHome("ProcessPaymentHome",ProcessPaymentHome.class);
        ProcessPayment process = ppHome.create();
        process.byCredit(customer, card, price);

        Ticket ticket = new Ticket(customer,cruise,cabin,price);
        return ticket;
         } catch(Exception e) {
                            throw new EJBException(e); ...

Get Enterprise JavaBeans, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.