EJB Transaction roll back in container managed transactions

1.3k views Asked by At

I am reading this blog about transaction handling in container managed transactions.

The author clearly demarcates the difference between EJB transaction and database transaction. A database transaction is just one of the EJB transactions.

Consider this example:

Take a money transaction. That is not only changing some numbers around in one or more databases. There is also administration, notification, confirmation and validation going on.

Based on this example consider the following stateless bean,

@Stateless
public class MoneyTransactionBean {

public void MoneyTransfer(int amount, BankAccount from, BankAccount to){

//db transaction
 /  // adminstration transaction //JMS
    // confirmation //JMS
    // Notification //JSP
    // validation //EJB
    // email //JMAIL
 }

}

Scenario 1: If the Notification step fails, does the JMS transaction associated with Confirmation get rolled back? In other words, will the JMS message be dequeued i.e Notification event is cleared from JMS queue?

Scenario 2: If all individual transactions (invoked on respective beans) are successful but Validation fails will the JMS message from the confirmation step be rolled back and will the email be dequeued?
How does the rollback of the transaction happen in this case?

1

There are 1 answers

4
NBW On

Because the JMS message is sent from within an EJB it is part of the same transactional context as the EJB, regardless of the setting used on the JMS session.

You state, "if all individual transactions are successful". Each of these steps are by default all part of the same transaction as MoneyTransfer and as such any exception (except for an @ApplicationException with rollback=false) will cause them to rollback.

The email step comes after the validation step so if the exception happens in validation no mail will have been sent.