Problem with EJB + POJO Helpers + EntitiyManager
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 manag开发者_JAVA技巧ed transactions.
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.
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_NEW
transaction attribute for the method of EJB3. So:
- when invoking
EJB3#bar()
, the container will suspend the transaction started forEJB2#foo()
and start a new transaction - when invoking the entity manager from
EJB3#bar()
, a new persistence context will be created. - since the transaction started for
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.
精彩评论