7 Replies Latest reply on Mar 11, 2009 11:45 AM by swd847

    why EntityManager is closed?

      I have POJO session-bean with:


      @Scope(ScopeType.SESSION)



      In this bean I have a property of:


      EntityQuery<something>



      This property I use to fill a datatable: values in resultList. This is standard decision I think.


      My question is EntityManager about which located in this EntityQuery. When I create an object of the EntityQuery in my bean I can get resultList correctly. It works without errors.


      But... Later in some method of the bean I want to refresh data of the EntityQuery. I want to refresh data using refresh method. And this method returns null of resultList because EntityManager is closed by this time. Hmm... To avoid this problem I must use this trick:



              if (!mfoPeriodList.getEntityManager().isOpen())
              {
                  EntityQuery query = new EntityQuery();  
                  EntityManager em = query.getEntityManager();
                  mfoPeriodList.setEntityManager(em);            
              }
              mfoPeriodList.refresh();



      Looks not good but I don't know how to do it better.
      Why EntityManager is closed in my EntityQuery? Is it bug or feature of EntityQuery? How to work with it correctly if I need to refresh data in some method?


      BTW... The same situation if I use extending EntityController instead of POJO. Please explain how to avoid closing of EntityManager in the bean.


      P.S.: Seam 2.1.1, Jboss 4.2.3


      Thanks.

        • 1. Re: why EntityManager is closed?
          swd847

          The entityManager is Conversation scoped, while your bean is session scoped. This means that after the conversation that created the bean is over, the entityManager will be closed.


          You should not store references to conversation scoped components inside a session scoped component (actually you should not store references to components at all). Instead they should be injection each time using @In.

          • 2. Re: why EntityManager is closed?

            You should not store references to conversation scoped components inside a session scoped component (actually you should not store references to components at all). Instead they should be injection each time using @In.


            Can you show me simple example how to do it with EntityManager?

            • 3. Re: why EntityManager is closed?
              swd847

              put this inside your component:



              @In EntityManaqer entityManager;



              That will inject the entityManager every time you invoke a method on the component

              • 4. Re: why EntityManager is closed?

                Stuart Douglas wrote on Mar 11, 2009 09:52:


                put this inside your component:


                @In EntityManaqer entityManager;



                That will inject the entityManager every time you invoke a method on the component


                I put this string in my bean. And IDE showed me this message:



                Undefined context variable 'entityManager'

                What is the problem? What should I do to solve it? May be I need some additional adjustments.

                • 5. Re: why EntityManager is closed?
                  swd847

                  In your components.xml you should have a line similar to:


                  <persistence:manager-persistence-context name="entityManager" />
                  



                  if your name is different rename the variable in the bean to whatever you called your entityManager. If it is called entityManager then just hit the run button and ignore the IDE error messsage.

                  • 6. Re: why EntityManager is closed?

                    Yes. It's working. Thanks a lot.
                    I think, the message of IDE is bug of IDE. Because injection of EntityManager is working.


                    And a last number of questions. As I uderstand, in session-scoped beans I must use setEntityManager method every time when I want to refresh data in EntityQuery. Is this true?
                    But how about extending EntityController in session-scoped bean?

                    • 7. Re: why EntityManager is closed?
                      swd847

                      Actually you should probably be injecting the EntityQuery using @In. You are going to have to post more details about what you are trying to achieve. Keeping entities lying around in session scope can be problematic because they will eventually become detached, which may cause problems depending on what you are trying to do.