I’m working with EJBs…I do the following and I don’t know why the injected EntityManager is not working as one might expect.
- EJB1 calls a method on EJB2 that writes to the DB.
- when EJB2 returns EJB1 sends a message to a MDB.
- MDB calls EJB3 that reads the DB and does some work.
My problem is that the EntityManager injected in all 3 EJBs with @PersistenceContext is not working properly. Calling persist() in EJB2 is not being reflected on the EntityManager injected in EJB3.
What might be wrong?
Hope I made my problem clear enough.
now working with Container managed transactions.
In a Java EE environment, the common case is to use a Transaction-Scoped Container-Managed entity manager. And with such an entity manager, the persistence context propagates as the JTA transaction propagates.
In your case, I suspect you’re using a
REQUIRES_NEWtransaction attribute for the method of EJB3. So:EJB3#bar(), the container will suspend the transaction started forEJB2#foo()and start a new transactionEJB3#bar(), a new persistence context will be created.EJB2#foo()has not yet committed, changes aren’t “visible” to the new persistence context.PS: Are you really creating new threads? If yes, little reminder: this is forbidden by the EJB spec.