2 Replies Latest reply on Dec 7, 2006 4:35 AM by Pete Muir

    LIE with Seam-managed transactions

    adsf adsf Newbie

      Hello,

      I somehow manage to get a LazyInitializationException when I use Seam-managed transactions.

      What I want to do is retrieve an id from a request, load the usecase with said id and make it available to my jsf page - just like the dvd.xhtml in the dvd store example does for dvds.

      When I use @Factory it works fine, but when I try to use page actions and @Out the usecase I get a LIE when it tries to access the first collection attribute (the application frame is generated with seam-gen and unmodified, I use Seam 1.1CR2 and JBoss AS 4.0.5).

      My Bean:

      @Stateful
      @Name("usecasesBean")
      public class UsecasesBean implements Usecases {
      
       @PersistenceContext
       private EntityManager em;
      
       @In(required=false, scope=ScopeType.EVENT)
       private String usecaseId;
      
       @Out(required = false)
       private Usecase usecase;
      
       @DataModel
       List<Usecase> usecases;
      
       @SuppressWarnings("unchecked")
       @Factory("usecases")
       public void findAllUsecases() {
       System.out.println("findAllUsecases...");
       System.out.println("usecaseId: " + usecaseId);
       usecases = (List<Usecase>) em.createQuery("Select u from Usecase u order by u.name").getResultList();
       }
      
       public void loadUsecaseById() {
       System.out.println("loadById...");
       if(usecaseId == null) {
       System.out.println("usecaseId was null");
       usecaseId = "1";
       }
       this.usecase = em.find(Usecase.class, Long.parseLong(usecaseId));
       System.out.println("Usecase: " + usecase.getName());
       }
      
      // @Factory("usecase")
      // public Usecase loadUsecaseById() {
      // System.out.println("loadById...");
      // if(usecaseId == null) {
      // System.out.println("usecaseId was null");
      // usecaseId = "1";
      // }
      // return em.find(Usecase.class, Long.parseLong(usecaseId));
      // }
      }


      My pages.xml:
      <pages>
       <page view-id="/showUsecase.xhtml" action="#{usecasesBean.loadUsecaseById}" />
      </pages>

      TransactionalSeamPhaseListener is loaded in faces-config.xml.

      When I exchange the loadUsecaseById() methods, comment the @Out usecase out and remove the pages.xml this works fine but the way above I get the following exception:
      01:35:21,631 INFO [STDOUT] findAllUsecases...
      01:35:21,635 INFO [STDOUT] usecaseId: null
      01:35:21,667 INFO [STDOUT] Hibernate: select usecase0_.ID as ID210_, usecase0_.summary as summary210_, usecase0_.standardWorkflow as standard7_210_, usecase0_.name as name210_, usecase0_.priority as priority210_, usecase0_.version as version210_, usecase0_.status as status210_ from Usecase usecase0_ order by usecase0_.name
      01:35:23,555 INFO [STDOUT] Filtering URL ...
      01:35:23,567 INFO [STDOUT] loadById...
      01:35:23,570 INFO [STDOUT] Hibernate: select usecase0_.ID as ID210_0_, usecase0_.summary as summary210_0_, usecase0_.standardWorkflow as standard7_210_0_, usecase0_.name as name210_0_, usecase0_.priority as priority210_0_, usecase0_.version as version210_0_, usecase0_.status as status210_0_ from Usecase usecase0_ where usecase0_.ID=?
      01:35:23,583 INFO [STDOUT] Usecase: Login User
      01:35:23,635 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: com.example.usecases.model.Usecase.actors, no session or session was closed
      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.usecases.model.Usecase.actors, no session or session was closed
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
       at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
       at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
       at javax.faces.model.ListDataModel.isRowAvailable(ListDataModel.java:84)
       at javax.faces.model.ListDataModel.setRowIndex(ListDataModel.java:97)
       at javax.faces.model.ListDataModel.setWrappedData(ListDataModel.java:111)
       at javax.faces.model.ListDataModel.<init>(ListDataModel.java:42)
       at com.sun.facelets.component.UIRepeat.getDataModel(UIRepeat.java:127)
       at com.sun.facelets.component.UIRepeat.setIndex(UIRepeat.java:305)
       at com.sun.facelets.component.UIRepeat.process(UIRepeat.java:333)
       at com.sun.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:617)
       at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:234)
       at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:580)
       at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:384)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
       at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
       at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
       at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
       at com.example.usecases.web.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:60)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:32)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:46)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
       at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
       at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
       at java.lang.Thread.run(Thread.java:595)
      01:35:23,640 ERROR [STDERR] 07.12.2006 01:35:23 com.sun.facelets.FaceletViewHandler handleRenderException
      SCHWERWIEGEND: Error Rendering View[/showUsecase.xhtml]
      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.usecases.model.Usecase.actors, no session or session was closed
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
       at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
       at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
       at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
       at javax.faces.model.ListDataModel.isRowAvailable(ListDataModel.java:84)
       at javax.faces.model.ListDataModel.setRowIndex(ListDataModel.java:97)
       at javax.faces.model.ListDataModel.setWrappedData(ListDataModel.java:111)
       at javax.faces.model.ListDataModel.<init>(ListDataModel.java:42)
       at com.sun.facelets.component.UIRepeat.getDataModel(UIRepeat.java:127)
       at com.sun.facelets.component.UIRepeat.setIndex(UIRepeat.java:305)
       at com.sun.facelets.component.UIRepeat.process(UIRepeat.java:333)
       at com.sun.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:617)
       at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:234)
       at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:239)
       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:580)
       at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:384)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
       at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
       at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
       at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
       at com.example.usecases.web.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:60)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:32)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:46)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
       at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
       at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
       at java.lang.Thread.run(Thread.java:595)
      01:35:23,676 ERROR [LazyInitializationException] could not initialize proxy - the owning Session was closed
      org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
       at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
       at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
       at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:198)
       at com.example.usecases.model.Workflow_$$_javassist_87.getWorkflowSteps(Workflow_$$_javassist_87.java)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:438)
       at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:82)
       at com.sun.facelets.el.LegacyELContext$LegacyELResolver.getValue(LegacyELContext.java:141)
       at com.sun.el.parser.AstValue.getValue(AstValue.java:117)
       at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:192)
       at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
       at com.sun.facelets.el.LegacyValueBinding.getValue(LegacyValueBinding.java:56)
       at com.sun.facelets.component.UIRepeat.getValue(UIRepeat.java:143)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at com.sun.facelets.util.DevTools.writeAttributes(DevTools.java:240)
       at com.sun.facelets.util.DevTools.writeStart(DevTools.java:284)
       at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:189)
       at com.sun.facelets.util.DevTools.writeComponent(DevTools.java:207)
       at com.sun.facelets.util.DevTools.debugHtml(DevTools.java:107)
       at com.sun.facelets.FaceletViewHandler.handleRenderException(FaceletViewHandler.java:677)
       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:646)
       at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:384)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
       at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
       at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
       at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
       at com.example.usecases.web.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:60)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamRedirectFilter.doFilter(SeamRedirectFilter.java:32)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.seam.servlet.SeamExceptionFilter.doFilter(SeamExceptionFilter.java:46)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
       at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
       at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
       at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
       at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
       at java.lang.Thread.run(Thread.java:595)
      

      As you can see the page function is action is correctly called and the usecase is loaded from the db but I get a LIE when I access the first collection attribute (actors.) in my jsf.

      Can someone please explain me why this happens.

      Thanks a lot in advance.

        • 1. Re: LIE with Seam-managed transactions
          adsf adsf Newbie

          I got it working by changing

          @PersistenceContext
          private EntityManager em;

          in
          @In(create = true)
          private EntityManager entityManager;


          But if used the wrong entitymanager why did it work for @Factory and not with @Out ?

          • 2. Re: LIE with Seam-managed transactions
            Pete Muir Master

            So a page action occurs when a page is requested, a @Factory lazy initialises the datamodel.

            In the @Factory case case the datamodel is requested (by the facelet), doesn't exist so instantiated by Seam (using @Factory) and used (without becoming detached).

            In the page action case the datamodel is instatiated by Seam when the page is requested, becomes detached, and is then requested by the page (and as an associated object is requested...)

            The page action case should work with an Extended persistence context, but, my advice is to always use Seam Managed Persistence Contexts as they work in more situations and have no downsides (I have seen at least).