Stateful bean injecting stateless bean, will they both use the same instance of EntityManagerFactory?

核能气质少年 提交于 2020-01-25 07:18:48

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/9114132/stateful-bean-injecting-stateless-bean-will-they-both-use-the-same-instance-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!