13 Replies Latest reply on Jul 7, 2008 9:32 AM by nimo stephan

    WARN EntityManager is closed by using s:convertEntity

    nimo stephan Master

      Hello,


      by using a s:convertEntity, I got the following exception:


      (My EntityManager is Seam-managed (@In) and is called entityManager)



      06:30:39,952 WARN  [lifecycle] EntityManager is closed
      java.lang.IllegalStateException: EntityManager is closed
           at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:42)
           at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:447)
           at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:442)
           at org.jboss.seam.persistence.EntityManagerProxy.joinTransaction(EntityManagerProxy.java:120)
           at org.jboss.seam.transaction.AbstractUserTransaction.enlist(AbstractUserTransaction.java:73)
           at org.jboss.seam.framework.EntityIdentifier.find(EntityIdentifier.java:31)
           at org.jboss.seam.framework.EntityIdentifier.find(EntityIdentifier.java:9)
           at org.jboss.seam.ui.AbstractEntityLoader.get(AbstractEntityLoader.java:30)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
           at java.lang.reflect.Method.invoke(Unknown Source)
           at org.jboss.seam.util.Reflections.invoke(Reflections.java:21)
           at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
           at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:31)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:38)
           at org.jboss.seam.util.Work.workInTransaction(Work.java:41)
           at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:32)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:42)
           at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
           at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
           at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:166)
           at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:102)
           at org.jboss.seam.ui.JpaEntityLoader_$$_javassist_1.get(JpaEntityLoader_$$_javassist_1.java)
           at org.jboss.seam.ui.EntityConverter.getAsObject(EntityConverter.java:80)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValues(MenuRenderer.java:483)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValuesForModel(MenuRenderer.java:169)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.convertSelectManyValue(MenuRenderer.java:95)
           at com.sun.faces.renderkit.html_basic.MenuRenderer.getConvertedValue(MenuRenderer.java:355)
           at javax.faces.component.UIInput.getConvertedValue(UIInput.java:934)
           at javax.faces.component.UIInput.validate(UIInput.java:860)
           at javax.faces.component.UIInput.executeValidate(UIInput.java:1065)
           at javax.faces.component.UIInput.processValidators(UIInput.java:666)
           at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
           at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1033)
           at javax.faces.component.UIForm.processValidators(UIForm.java:229)
           at org.ajax4jsf.component.AjaxViewRoot$3.invokeContextCallback(AjaxViewRoot.java:435)
           at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:235)
           at org.ajax4jsf.component.AjaxViewRoot.processValidators(AjaxViewRoot.java:451)
           at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:100)
           at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
           at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
           at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:73)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:42)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:154)
           at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:260)
           at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:366)
           at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:493)
           at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
           at java.lang.Thread.run(Unknown Source)
      06:30:39,952 WARN  [lifecycle] executePhase(PROCESS_VALIDATIONS 3,com.sun.faces.context.FacesContextImpl@f250f3) threw exception
      javax.faces.FacesException: EntityManager is closed
           at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:108)
           at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
           at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
           at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:73)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:42)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
           at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:154)
           at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:260)




      How can I solve that? When I do not use the s:convertEntity, my entityManager is open. When I use it, the it s closed. I use the actual SEAM 2.1.

        • 1. Re: WARN EntityManager is closed by using s:convertEntity
          nimo stephan Master

          The s:convertEntity-Tag does even not work, if I will init in the components.xml with the name entityManager:



          <component name="entityConverter" scope="CONVERSATION" precedence="20"             class="org.jboss.seam.ui.converter.EntityConverter">
          <property name="entityManager">#{entityManager}</property>
          </component>



          From one failure to another:



          12:13:46,729 ERROR [[/vONE]] Exception sending context initialized event to listener instance of class org.jboss.seam.servlet.SeamListener
          java.lang.RuntimeException: error while reading /WEB-INF/components.xml
                  at org.jboss.seam.init.Initialization.initComponentsFromXmlDocument(Initialization.java:158)
                  at org.jboss.seam.init.Initialization.create(Initialization.java:108)
                  at org.jboss.seam.servlet.SeamListener.contextInitialized(SeamListener.java:34)
                  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3856)
                  at org.apache.catalina.core.StandardContext.start(StandardContext.java:4361)
                  at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:790)
                  at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:770)
                  at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:553)
                  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                  at java.lang.reflect.Method.invoke(Unknown Source)
                  at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:296)
                  at org.jboss.mx.server.RawDynamicInvoker.invoke(RawDynamicInvoker.java:164)
                  at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)



          I red



          The other thing is. For what do I need the s:convertEntity-Tag, when it works without it?


          Look at that. I generate a list for my menu:


            
          @Factory("personList")
          public List <Person> getTeamList() {
          
          return  entityManager.createQuery("from Person p").getResultList();
          }



          This works:


                 
          <h:selectOneMenu value ="#{person.idPerson}">
          <s:selectItems value="#{personList}" var="p" label="#{p.name}" noSelectionLabel="Please Select.."/>
          </h:selectOneMenu>
          



          and this works NOT (stacktrace 'EntityManager is closed' occurs):


                 
          <h:selectOneMenu value ="#{person.idPerson}">
          <s:selectItems value="#{personList}" var="p" label="#{p.name}" noSelectionLabel="Please Select.."/>
          <s:convertEntity />
          </h:selectOneMenu>
          



          Huhh?


          I have red all the stuff like http://www.seamframework.org/Community/SselectItemsAndSconvertEntity , the tutorials, the examples (seamBay and the like), the seam-docu, and so on..but without success..for me, it does not work when I explicit declare the s:convertEntity-Tag. (I use a Conversation-Scope for my business-bean.)


          Any ideas??


          • 2. Re: WARN EntityManager is closed by using s:convertEntity
            nimo stephan Master

            I cannot use  s:convertEntity  for ALL my components. It does not matter, what for component. I get every time the same failure as above...Entity Manager is closed...so HOW can I open it..I have thought, it should be enough to Inject it with



              @In (create=true)
              private EntityManager entityManager;



            use a Seam managed transactions with



            <transaction:ejb-transaction />



            and the



            <s:convertEntity />

            -Tag
            .


            What have I missed? I there something else, what I should consider??

            • 3. Re: WARN EntityManager is closed by using s:convertEntity
              nimo stephan Master

              In the SEAM-Reference it s said (p. 205):



              Seam transaction management is enabled by default for all JSF requests. ..

              I tried to set it up manually without success:



              <core:init transaction-management-enabled="true"/>




              The Message EntityManager is closed allways occurs, when I use the s:convertEntity-Tag. So I do not understand, what s goin on?


              (I use SEAM 2.1, Hibernate 3.2.x, JBoss 4.2.x)

              • 4. Re: WARN EntityManager is closed by using s:convertEntity
                Daniel Hinojosa Master

                Did you start the conversation, or did you close it somehow?...


                Comment this out components.xml for now.


                <component name="entityConverter" scope="CONVERSATION" precedence="20"             class="org.jboss.seam.ui.converter.EntityConverter">
                <property name="entityManager">#{entityManager}</property>
                </component>
                



                How are you starting a conversation on this page?

                • 5. Re: WARN EntityManager is closed by using s:convertEntity
                  nimo stephan Master

                  hello,


                  I commented this out:





                  I start a conversation with that:


                  @Factory("startConv") @Begin(join = true)
                    public void start()
                    {...
                  }




                  and have two possible ends:



                  @End
                  public void cancel() {..}






                  @End
                  public void submit() {..}




                  and last (but not least) that:



                    @Destroy @Remove
                    public void destroy() {}



                  • 6. Re: WARN EntityManager is closed by using s:convertEntity
                    Daniel Hinojosa Master

                    Wow, never saw that before..


                    How are you triggering the start() method?
                    What component is startConv and how is it mapped?

                    • 7. Re: WARN EntityManager is closed by using s:convertEntity
                      nimo stephan Master

                      The start-method is triggered by visiting a site..it triggers itself via the factory-method. For instance, look at the seamspace-example..'RegisterAction.java':


                      @Factory("newMember") @Begin(join = true)
                         public void start()
                         {
                            newMember = new Member();
                         }
                      



                      I do it in a similar way:


                        @Factory("startConv") @Begin(join = true)
                        public void newPerson()
                        {
                                person= new Person();
                                role = new Role();
                      
                        }



                      The 'startConv' is never called explicitly..cos the factory does it.


                      I have many other factory-methods inside my bean to get data into my components (such as SelectOne, SelectMany, etc):


                      @Factory("selectOneRoleList")
                        public List <Role> getAllRoles() {
                      return entityManager.createQuery("from Role r").getResultList();
                       }
                      



                      I assign the Factory 'selectOneList' to a GUI-component:


                      <h:selectOneMenu value ="#{person.idRole}">
                      <s:selectItems value="#{selectOneRoleList}" var="r" label="#{r.roleName}" itemValue="#{r.idRole}"/>
                      </h:selectOneMenu>
                      



                      To assign the value (getting from the view), in my session-Bean, I can say:


                      @End
                      public void submit() {
                      ...
                      person.setIdRole(this.person.getIdRole());
                      ...
                      entityManager.persist(person);
                      
                      }




                      Notice, I have NEVER assigned the s:convertEntity-Tag in my GUI-Component, but I am able to assign the actual GUI-instance of a person into my injected person-Instance in my session-Bean.



                      But the strange thing is:


                      When I explicitly assign the s:convertEntity-Tag into my GUI-Component, then the submit-Method does NOT work, cos the message 'EntityManager is closed' appears.


                      My Entity-Beans are mapped in an common way and are available as Seam-Components (Scope.CONVERSATION as default).






                      • 8. Re: WARN EntityManager is closed by using s:convertEntity
                        nimo stephan Master

                        I have to say, that my Entity-Object 'Person.java' is declared as follows:


                        @Entity
                        @Name("person")
                        public class Person implements Serializable{
                        
                        ...
                        }




                        In my Session-Bean, I injected it as follows:


                        @In (required=false) @Out(required=false)
                        private Person person;
                        





                        And in my View, I referenced it as follows:


                        For example, an input-Text:


                        ...
                        <h:inputText value="#{person.name}" id="name_InputText" required="true"/>
                        ...
                        ...
                        <h:selectOneMenu value ="#{person.idRole}">
                        <s:selectItems value="#{selectOneRoleList}" var="r" label="#{r.roleName}" itemValue="#{r.idRole}"/>
                        </h:selectOneMenu>
                        ...
                        
                        



                        So u see, in all three components, it has the same instance-name: 'person'. (Is this my failure?)


                        Should I inject it with a other name?:


                        @In (required=false) @Out(required=false)
                        private Person myPerson;
                        


                        and reference it in my GUI with 'myPerson' ?


                        • 9. Re: WARN EntityManager is closed by using s:convertEntity
                          Daniel Hinojosa Master

                          But I think IMHO, that's the problem.


                          The reason why that would work on seamspace is because in view/register.xhtml, #{newMember} is actually needed. And when it is needed, seam looks for a method with @Factory(newMember) and creates that object.


                          Do you have something in a page that is looking for using a #{startConv} component?


                          If not, then your conversation probably isn't starting up.
                          If you are creating a new person and want to use the factory then I might recommend...


                           @Factory("person") @Begin(join = true)
                            public void newPerson()
                            {
                                    person= new Person();
                                    role = new Role();
                          
                            }
                          





                          • 10. Re: WARN EntityManager is closed by using s:convertEntity
                            nimo stephan Master

                            Yes,
                            Good morning :-)


                            that was the reason ! I came and changed it in this way:


                            I inject it with a other name:


                            @In
                            Persons thisPerson;



                            and referenced this instance in my view:


                            <h:selectOneMenu value ="#{thisPerson.idRole}">



                            and NOW I can use the s:convertEntity :-)


                            The reason for the message EntityManager is closed was,
                            I referenced the view with my entityBean in a direct way (the same instance and seam-name 'person'), and my entityBean has sensefully no entityManager open.


                            I am so glad that it works:-)


                            One question:


                            I have a selectOneMenu. The User must not select an item:


                            <h:selectOneMenu value ="#{selectedRoles}" required="false">
                            <s:selectItems value="#{roleList}" var="r" label="#{r.desc}" id="selectTeam" noSelectionLabel="Please select (optional)"/>
                            <s:convertEntity/>
                            </h:selectOneMenu>



                            With s:convertEntity-Tag it returns nothing, when the User has no item selected. But I get a


                            Caused by: java.lang.NullPointerException
                                    at sessionBean.submit(SessionBean.java:51)



                            which points to this line:


                            myPerson.setIdRole(myRole.getIdRole());



                            In my EntityBean, it is possible to get or set Null-Values as I have mapped the idRole to the Wrapper Integer (which can be null).


                            Is there a property to avoid this NullPointerException? I have assigned the


                            required="false"

                            to s:selectItem, but it does not work.


                            (Indeed, I can catch this failure, but it seems not so elegant..)








                            • 11. Re: WARN EntityManager is closed by using s:convertEntity
                              nimo stephan Master

                              The strange thing is,


                              when I assign it directly with a null-Value:


                              myPerson.setIdRole(null);



                              then NO exception occurs (in the database, a null-value is assigned)


                              But when I assign it with:


                              myPerson.setIdRole(myRole.getIdRole());



                              then 'java.lang.NullPointerException' occurs even 'myRole.getIdRole()' is null.


                              So I assume, the Role-Object is empty, when no Role is selected. (The convertEntity cannot convert a null-Value..?!)

                              • 12. Re: WARN EntityManager is closed by using s:convertEntity
                                Daniel Hinojosa Master

                                Something else I think is missing...and you keep changing your class, that I don't what has what anymore ;)


                                What does the Person and Role class look like including their relationship to one another?


                                • 13. Re: WARN EntityManager is closed by using s:convertEntity
                                  nimo stephan Master

                                  Hello Daniel,


                                  now, I got it:


                                  I have injected my Role via:


                                    
                                  @In (required=false) @Out(required=false)
                                  private Role thisRole;
                                  


                                  instead of


                                  private Role thisRole;
                                  
                                  public Team getThisRole() {
                                          return thisRole;
                                  }
                                  
                                  public void setThisRole(Team thisRole) {
                                          this.thisRole= thisRole;
                                  }
                                  



                                  and now, it works well also with no selection of roles.


                                  At first, I wanted to use the getters/setters to avoid Bijection and have forgotten to proof the list for emptyness, but now I biject the Role class with the possibility to be empty by declaring


                                  Out(required=false)



                                  thanks