EJB 的问题 + POJO 助手 +实体管理器
我正在使用 EJB...我执行了以下操作,但我不知道为什么注入的 EntityManager 没有按预期工作。
- EJB1 调用 EJB2 上写入数据库的方法。
- 当 EJB2 返回时,EJB1 向 MDB 发送一条消息。
- MDB 调用 EJB3 来读取 DB 并执行一些工作。
我的问题是,使用 @PersistenceContext 注入所有 3 个 EJB 中的 EntityManager 无法正常工作。在 EJB2 中调用 persist() 不会反映在 EJB3 中注入的 EntityManager 上。 可能出了什么问题? 希望我的问题足够清楚。 现在使用容器管理的事务。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Java EE 环境中,常见的情况是使用事务范围的容器管理实体管理器。通过这样的实体管理器,持久性上下文会随着 JTA 事务的传播而传播。
就您而言,我怀疑您正在为 EJB3 的方法使用
REQUIRES_NEW
事务属性。因此:EJB3#bar()
时,容器将暂停为EJB2#foo()
启动的事务,并调用实体管理器时 启动新事务>EJB3#bar()
,将创建一个新持久化上下文。EJB2#foo()
启动的事务尚未提交,因此更改对于新的持久性上下文“不可见”。PS:你真的在创建新线程吗?如果是,请注意:这是 EJB 规范所禁止的。
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: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.