EJB 的问题 + POJO 助手 +实体管理器

发布于 2024-09-30 12:23:56 字数 349 浏览 18 评论 0原文

我正在使用 EJB...我执行了以下操作,但我不知道为什么注入的 EntityManager 没有按预期工作。

  1. EJB1 调用 EJB2 上写入数据库的方法。
  2. 当 EJB2 返回时,EJB1 向 MDB 发送一条消息。
  3. 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.

  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 managed transactions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-10-07 12:23:56

我的问题是,使用 @PersistenceContext 注入所有 3 个 EJB 中的 EntityManager 无法正常工作。在 EJB2 中调用 persist() 不会反映在 EJB3 中注入的 EntityManager 上。

在 Java EE 环境中,常见的情况是使用事务范围的容器管理实体管理器。通过这样的实体管理器,持久性上下文会随着 JTA 事务的传播而传播。

就您而言,我怀疑您正在为 EJB3 的方法使用 REQUIRES_NEW 事务属性。因此:

  • 当调用 EJB3#bar() 时,容器将暂停为 EJB2#foo() 启动的事务,并
  • 在从 调用实体管理器时 启动新事务>EJB3#bar(),将创建​​一个持久化上下文。
  • 由于为 EJB2#foo() 启动的事务尚未提交,因此更改对于新的持久性上下文“不可见”。

PS:你真的在创建新线程吗?如果是,请注意:这是 EJB 规范所禁止的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文