4 Replies Latest reply: Jan 19, 2009 8:59 PM by 800448 RSS

    JPA with JSF and Concurrency

    800448
      Hi,

      I have a JSF web application where I configure 2 layers. Controllers performing presentation logic, Services handling transactions and DAOs performing actual access of entities.

      The Services and DAOs are configured as application scoped managed beans. The controllers are defined with request or session scope based on its purpose.

      I have used annotations to inject UserTransaction into Services and EntityManager (not EntityManagerFactory) into DAOs. One service method can access more than one DAO.

      I read that EntityManager is not thread safe. So is it a better to inject EntityManagerFactory into DAOs rather than EntityManager? If so, what overhead will creating and closing multiple EntityManager instances within a single transaction cause?
        • 1. Re: JPA with JSF and Concurrency
          807589
          Normally, the lifecycle of an EntityManager (EM) is bound by a "logical" transaction (a unit or work). A long logical transaction may span several database transactions. EM acts as a session cache for entities it operates on. It seems that your "service" classes drive the logical transactions. Probably a method in your service class is mapped to one logical transaction or a unit of work. In such a case I'd keep my DAO stateless by having its methods take the EM as an argument instead of making the EM a member variable or creating it afresh in each DAO method. Passing EM as argument to DAO methods will ensure thread safety and also minimize the overhead incurred in creating and throwing away the EM (i.e. the cached entities in a session) in every DAO method.

          This link may be of help too: [https://blueprints.dev.java.net/bpcatalog/ee5/persistence/webonlyapp.html|https://blueprints.dev.java.net/bpcatalog/ee5/persistence/webonlyapp.html]

          HTH,
          Balwinder
          • 2. Re: JPA with JSF and Concurrency
            800448
            Thanks a ton for that URL.

            BTW, when using Stateless EJBs and injecting EM into it using annotation, how does the container keep it thread safe? If I'm not wrong, a single instance of a Stateless session bean may be used to serve two concurrent requests.
            • 3. Re: JPA with JSF and Concurrency
              807589
              Injecting EM into a stateless session EJB won't make the EM (or operations using it) thread safe. The point is that if your EM ends up as an instance variable (i.e. object state) of a class (your service class or a stateless session EJB) then thread safety of the containing class is not guaranteed.

              In a normal Java program, I'd not keep a thread-unsafe object as a member variable of a class (otherwise I'll have to synchronize access to that thread-unsafe object).
              • 4. Re: JPA with JSF and Concurrency
                800448
                Most JPA tutorials say container takes care of injecting right EM into stateless beans. Also when I tried it out, for concurrent requests the container was automatically giving a new instance of the stateless bean itself. However, for a non-concurrent request, it was using a pooled instance.