javajpajtamessage-driven-bean

Combining MDB, JPA and JTA


I'm developing a system to process messages and update the database accordingly, but I need to keep some degree of isolation between layers. I have in mind something like the following.

One single execution of MyMdb.onMessage() needs to perform several accesses to the database, both read and write.

What is the right scenario? Am I missing something?


Solution

  • Entity managers injected in a stateless EJB in the way that you described are exactly what you should do. This type of injection provides a 'Container-Managed Entity Manager' which is 'transaction scoped'. So in the scenario that you describe.

    1. the onMessage MDB call will create a transaction
    2. the call to the stateless bean will happen to the same transaction context, creating an Entity Manager which will live until the transaction finishes usually when the MDB method returns.

    The specific type of injected entity manager for the same EJB instance doesn't survive and is not re-used across different transactions.