3 Replies Latest reply: May 9, 2014 3:18 AM by gimbal2 RSS

    does container managed entity manager is thread-safe in stateless session bean?

    Scofield Wang

      Hi,

       

      I read JEE6 doc and confused with

       

      Does container managed entity manager (injected by PersistenceContext annotation) is thread-safe in stateless session bean in mutiple-thread env?

       

      See code below, if there are 2 requests to stateless sesion bean in 2 concurrent threads ,  is it using same Entity Manager Instance or not?

       

      @Stateless(name = "HRFacade", mappedName = "HR_FACES_EJB_JPA-HRFacade-HRFacade")

      public class HRFacadeBean implements HRFacade, HRFacadeLocal {

          @Resource

          SessionContext sessionContext;

         

          @PersistenceContext(unitName = "HRFacade")

          private EntityManager em;

       

          public Locations persistLocations(Locations locations) {
              em.persist(locations);
              return locations;
          }

          public Locations mergeLocations(Locations locations) {
              return em.merge(locations);
          }

          public void removeLocations(Locations locations) {
              locations = em.find(Locations.class, locations.getLocationId());
              em.remove(locations);
          }

          /** <code>select o from Locations o</code> */
          @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
          public List<Locations> getLocationsFindAll() {
              return em.createNamedQuery("Locations.findAll", Locations.class).getResultList();
          }

      }

        • 1. Re: does container managed entity manager is thread-safe in stateless session bean?
          gimbal2

          Yes its safe. Stateless beans would be utterly useless if that wasn't the case. That's the "container managed" bit of the description, as long as you let the container do its job, you don't have to worry about these kind of details.

          • 2. Re: does container managed entity manager is thread-safe in stateless session bean?
            Scofield Wang

            thank you. I also verify this point, every invocation to a business method of  stateless sesion beans will get a new entity manager instance.

             

            Can I inject mutiple entity managers in stateless session bean to use them in a JTA transaction? it can work well?

            • 3. Re: does container managed entity manager is thread-safe in stateless session bean?
              gimbal2

              Scofield Wang wrote:

               

              thank you. I also verify this point, every invocation to a business method of  stateless sesion beans will get a new entity manager instance.

               

              Can I inject mutiple entity managers in stateless session bean to use them in a JTA transaction? it can work well?

              Question 1: that fully depends on your transaction management scheme. If you design your methods such that transactions are adopted, then the entity manager and its persistence store are going to be adopted as well. But if you annotate a method to have its own isolated transaction (CREATE_NEW for example), then it will get its own EntityManager wrapped around that isolated transaction.

               

              Question 2: Very good question. It shouldn't be a problem. I'm not 100% sure since I tend to isolate the management of a persistence unit to its own set of beans and not mix and mash. It keeps the application design simpler.

               

              I would really advise you to get a good book on EJB tech to get a better feel for not only the technology but also the theory behind the technology. EJBs are deceptively easy to program but sadistically hard to properly apply with all the stuff that is going on under the hood.