开发者

Problem with EJB + POJO Helpers + EntitiyManager

开发者 https://www.devze.com 2023-01-23 19:02 出处:网络
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.

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.

  1. EJB1 calls a method on EJB2 that writes to the DB.
  2. when EJB2 returns EJB1 sends a message to a MDB.
  3. 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 for EJB2#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.

0

精彩评论

暂无评论...
验证码 换一张
取 消