有状态Bean注入无状态Bean,它们是否都将使用EntityManagerFactory的相同实例? [英] Stateful bean injecting stateless bean, will they both use the same instance of EntityManagerFactory?

查看:123
本文介绍了有状态Bean注入无状态Bean,它们是否都将使用EntityManagerFactory的相同实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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() {      
   }
}

欢迎任何答案.

致谢

推荐答案

是的,它们将得到相同的一个:在"MY_PU"名称下定义的一个.他们还能去哪家工厂?

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

这篇关于有状态Bean注入无状态Bean,它们是否都将使用EntityManagerFactory的相同实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆