Basically, I have a JMS queue and an MDB to collects the messages from the JMS queue, does some processing on them, and then persists the messages into the database via JPA. I marked the method, which is responsible for persisting the message into DB, to be started in a new transaction:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void create(T entity)
{
try
{
getEntityManager().persist(entity);
}
catch(Exception e)
{
throw new RuntimeException("DB Exception");
}
}
If a transaction is rolled back, is it going to be retired automatically until the transaction completes? If not, how to enable that?
If the exception propagates to the MDB, the transaction will not commit, the message will not be acknowledged as received and will be retried. From the EJB 3.1 specs:
Message acknowledgment is automatically handled by the container. If the message-driven bean uses container-managed transaction demarcation, message acknowledgment is handled automatically as a part of the transaction commit.
I'm not familiar with Weblogic but there should be a JMS queue parameter that set the number of retries, retry interval, etc. until the message is dropped or put on an undelivered queue.
But usually is better to catch the exception in the MDB because a RuntimeException thrown from the MDB results in the bean to discarded by the container. From the EJB 3.1 specs:
Message-driven beans should not, in general, throw RuntimeExceptions.
A RuntimeException that is not an application exception thrown from any method of the message-driven bean class (including a message listener method and the callbacks invoked by the container) results in the transition to the “does not exist” state.
For example, is better to have:
public class MyMDB implements MessageListener {
@Resource
private MessageDrivenContext context;
public void onMessage() {
try {
//some other processing
someService.create(entity);
}
catch(Exception e) {
//mark the message as undelivered
context.setRollbackOnly();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With