有状态bean注入无状态bean,它们会使用同一个EntityManagerFactory实例吗?
我有一个有状态 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,他们将得到相同的:在“MY_PU”名称下定义的那个。他们还能得到哪家工厂?
Yes, they will get the same one: the one defined under the "MY_PU" name. Which other factory could they get?