Question

I have an MDB that calls some methods in its onMessage() method.Inside the catch block of onMessage() I run an update query using preparestatement.I notice that if the flow reaches the catch block it does not commit the update statement.Is it not possible to do that inside the catch block of an MDB?My onMessage() methos is like this

public void onMessage(Message message) {

try{ someMethod() } catch(Throwable o) { someUpdateMethod()//update query runs here } }

Was it helpful?

Solution

Whatever threw the exception caused the transaction context into rollback only mode. When onMessage returns all transactional resources will be called to rollback. This includes the datasource used for the prepared statement in someUpdateMethod().

To get the update committed it must be executed in a separate transaction. Do this by calling another stateless session bean with the @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) on the method.

@MessageDriven(...
public class MyMdb inpelements MessageListener
    @EJB
    Updater updater;

    @Override
    public void onMessage(Message message) {
      try {
          someMethod();
      }
      catch(Throwable o) {
         updater.someUpdateMethod();
      } 
    }
}

The stateless session EJB that executes the update in a separate transaction.

@Stateless
public class Updater {
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public String someUpdateMethod() {
        //update query runs here
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top