有状态bean注入无状态bean,它们会使用同一个EntityManagerFactory实例吗?

发布于 2025-01-02 00:15:07 字数 721 浏览 3 评论 0原文

我有一个有状态 Bean,注入一个 JPA PersistenceUnit 和另一个无状态 bean。无状态 bean 也注入相同的 PersistenceUnit。我的问题是,EJB 容器是否会在两个 bean 中注入相同的 PersistenceUnit 实例。我必须非常确定这里的行为。

@Stateful
public class MyStatefulBean {

   @PersistenceUnit(unitName = "MY_PU")
   private EntityManagerFactory emf;

   @EJB
   MyStatelessLocal statelessEJB;

   public void doSomething() {
     // Question will statelessEJB use the same instance of EntityManagerFactory? 
     statelessEJB.doSomthingWithEntityManager();
   }
}


@Stateless
public class MyStatelessBean {

   @PersistenceUnit(unitName = "MY_PU")
   private EntityManagerFactory emf;

   public void doSomthingWithEntityManager() {      
   }
}

欢迎任何答案。

问候

I have a Stateful Bean injecting a JPA PersistenceUnit and another stateless bean. The stateless bean is injecting the same PersistenceUnit as well. My question is, will the EJB container inject the same instance of PersistenceUnit in both beans. I have to be very sure about the behaviour here.

@Stateful
public class MyStatefulBean {

   @PersistenceUnit(unitName = "MY_PU")
   private EntityManagerFactory emf;

   @EJB
   MyStatelessLocal statelessEJB;

   public void doSomething() {
     // Question will statelessEJB use the same instance of EntityManagerFactory? 
     statelessEJB.doSomthingWithEntityManager();
   }
}


@Stateless
public class MyStatelessBean {

   @PersistenceUnit(unitName = "MY_PU")
   private EntityManagerFactory emf;

   public void doSomthingWithEntityManager() {      
   }
}

Any answers are welcome.

Regards

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

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

发布评论

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

评论(1

你怎么敢 2025-01-09 00:15:07

是的,他们将得到相同的:在“MY_PU”名称下定义的那个。他们还能得到哪家工厂?

Yes, they will get the same one: the one defined under the "MY_PU" name. Which other factory could they get?

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