Chapter 8. Transactions

ACID Transactions

To understand how transactions work, we will revisit the TravelAgent bean, a stateful session bean that encapsulates the process of making a cruise reservation for a customer. Here is the TravelAgent’s bookPassage() method:

public Ticket bookPassage(CreditCard card, double price)
    throws IncompleteConversationalState {
    // EJB 1.0: also throws RemoteException
                
    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) {
        // EJB 1.0: throw new RemoteException("",e);
        throw new EJBException(e);
    }
}

The TravelAgent bean is a fairly simple session bean, and its use of other beans is a typical example of business object design and workflow. Unfortunately, good business object design is not enough to make these beans useful in an industrial-strength application. The problem is not with the definition of the beans or the workflow; the problem is that a good design doesn’t, in and of itself, guarantee that the TravelAgent’s bookPassage() method represents a good ...

Get Enterprise JavaBeans, Second 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.