Think about it.

image with no caption

If you want your message acknowledgement tied to the status of the transaction, use container-managed transactions (CMT). Most of the time, this will be your choice.

But if you want to decouple message-acknowledgement from the status of your transaction, your only choice is to use bean-managed transaction demarcation (BMT). With BMT, the Container looks only at whether your method completes. Method completes? Fine, the message acknowledgement stands. Runtime exception? Message goes back in the queue.

But what if you have a scenario where the bean can never commit, because of something inherently bad about the message? With CMT, that message will keep coming and coming and coming and... but not forever. Most messaging services let you configure how many times a particular message is resent before the service says, “This message looks like poison; let’s send it to a Special Place (like a Bad Message queue) where it can’t harm anyone”. You really need to watch out for this, because there’s nothing in the spec to help. You’re reliant on your server vendor!

There is another possibility, though. With BMT, you could write your method in such a way that you rollback the method, but still finish the method. That way, the Container just goes whistling on its merry way thinking everything was fine. It has no idea that things went badly. In this scenario, you’d catch whatever exception ...

Get Head First EJB 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.