0 Replies Latest reply on Jul 23, 2007 10:44 PM by David MacBride

    Seam-injected EntityManager, but still LazyInitializationExc

    David MacBride Newbie

      Hi,

      I'm getting a hibernate LazyInitializationException in one of my Session beans. What is really confusing me is the bean has access to a Seam-injected EntityManager instance that seems to work.
      In the following code listing, the error occurs when calling "this.listOfTracksInThisRequest = request.getTracks()" in the getDefectsForThisRequest() method. Note that just above I am accessing the EntityManager in the call fixmgmtEntityManager.merge(request).

      /*
       *-------------------------------------------------------------------
       * IBM Confidential
       * OCO Source Materials
       *
       * WebSphere Commerce
       *
       * (c) Copyright International Business Machines Corporation. 2007
       * All rights reserved.
       *
       * The source code for this program is not published or otherwise
       * divested of its trade secrets, irrespective of what has been
       * deposited with the US Copyright Office.
       *-------------------------------------------------------------------
       */
      /**
       *
       */
      package my.application.fixmgmt.model.actions;
      
      import java.io.Serializable;
      import java.util.Set;
      
      import javax.ejb.Local;
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.persistence.EntityManager;
      
      import org.hibernate.validator.InvalidStateException;
      import org.hibernate.validator.InvalidValue;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Create;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.datamodel.DataModelSelection;
      import org.jboss.seam.log.Log;
      
      import my.application.fixmgmt.model.Request;
      import my.application.fixmgmt.model.Track;
      
      /**
       * @author Lucas Brodzinski
       *
       */
      @Local
      @Stateful
      public class displayRequestPageEditStatusAction implements displayRequestPageEditStatus,Serializable {
      
       @In EntityManager fixmgmtEntityManager; //where fixmgmtEntityManager is defined in components.xml
      
       @In @Out
       private Request request;
      
       /** The request object as stored in the database before any modification is done to it. We
       * keep a copy of this so we can determine what changes where made to the bean after the "save"
       * button is hit.*/
       private Request initialRequest;
      
       @DataModel
       private Set<Track> listOfTracksInThisRequest;
      
       @DataModelSelection
       @Out(required=false)
       private Track track;
      
       @Logger
       private Log log;
      
       public void editButtonClickAction()
       {
       log.info("editButtonClickAction: start");
       this.initialRequest = Request.copyRequest((Request)fixmgmtEntityManager.createQuery("select request1 from Request request1 where request1.id in ('" + this.request.getId() + "')").getResultList().get(0));
       log.info("Retrived initial request with id: " + this.initialRequest.getId());
       log.info("editButtonClickAction: end");
       }
      
       public String saveButtonClickAction()
       {
       log.info("saveButtonClickAction: start");
       this.auditReportImpl();
       try {
       fixmgmtEntityManager.merge(request);
       fixmgmtEntityManager.flush();
       } catch (InvalidStateException ise) {
       log.info("InvalidStateException.getMessage = " + ise.getMessage());
       InvalidValue theInvalidValues[] = ise.getInvalidValues();
       for(int i=0; i <theInvalidValues.length; i++) {
       log.info("InvalidValue.getMessage = " + theInvalidValues.getMessage());
       }
       ise.printStackTrace();
       }
       log.info("saveButtonClickAction: end");
       return "main";
       }
      
       public void auditReportImpl()
       {
       log.info("auditReportImpl: start");
       String auditTrail = this.request.getAuditTrail();
       if(!this.initialRequest.getProblemAbstract().equals(this.request.getProblemAbstract()))
       {
       auditTrail = auditTrail + "\nProblem abstract changed from: \"" + this.initialRequest.getProblemAbstract() + "\" to \"" + this.request.getProblemAbstract() + "\".\n";
       }
       if(!this.initialRequest.getSuggestedAPARNumber().equals(this.request.getSuggestedAPARNumber()))
       {
       auditTrail = auditTrail + "\nSuggested APAR number changed from: " + this.initialRequest.getSuggestedAPARNumber() + " to: " + this.request.getSuggestedAPARNumber() + ".\n";
       }
       if(!this.initialRequest.getPmr().equals(this.request.getPmr()))
       {
       auditTrail = auditTrail + "\nPMR number changed from: " + this.initialRequest.getPmr() + " to: " + this.request.getPmr() + ".\n";
       }
       if(!this.initialRequest.getCurrentTargetDate().equals(this.request.getCurrentTargetDate()))
       {
       auditTrail.concat("\nCurrent target date changed from: " +
       this.initialRequest.getCurrentTargetDate().toString() + " to: " +
       this.request.getCurrentTargetDate().toString() +
       ".\n");
       }
       this.request.setAuditTrail(auditTrail);
       log.info("Request audit trail: \n" + this.request.getAuditTrail());
       log.info("auditReportImpl: end");
       }
      
       public displayRequestPageEditStatusAction()
       {
      
       }
      
       @Destroy @Remove
       public void destroy() { }
      
       @Factory("listOfTracksInThisRequest")
       public void getDefectsForThisRequest()
       {
       log.info("Creating list of defects for request with id: " + this.request.getId());
       fixmgmtEntityManager.merge(request); // to see if I can access the EntityManager - this line works!
       fixmgmtEntityManager.flush(); // to see if I can access the EntityManager - this line works!
       if (fixmgmtEntityManager.isOpen()){
       log.info("getDefectsForThisRequest: fixmgmtEntityManager is open"); // this stsmt gets printed!
       }
       this.listOfTracksInThisRequest = request.getTracks(); // throws "org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: my.application.fixmgmt.model.Request.tracks, no session or session was closed"
       }
       }
      


      The stack trace I get in the browser and log is:

      javax.ejb.EJBTransactionRolledbackException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ibm.commerce.fixmgmt.model.Request.tracks, no session or session was closed
      at org.jboss.ejb3.tx.Ejb3TxPolicy.handleInCallerTx(Ejb3TxPolicy.java:93)
      at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:130)
      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:201)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:83)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
      at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:131)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.stateful.StatefulContainer.localInvoke(StatefulContainer.java:203)
      at org.jboss.ejb3.stateful.StatefulLocalProxy.invoke(StatefulLocalProxy.java:98)
      at $Proxy369.getDefectsForThisRequest(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:615)
      at org.jboss.seam.util.Reflections.invoke(Reflections.java:20)
      at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:31)
      at org.jboss.seam.intercept.ClientSideInterceptor$1.proceed(ClientSideInterceptor.java:72)
      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:57)
      at org.jboss.seam.interceptors.RemoveInterceptor.aroundInvoke(RemoveInterceptor.java:40)
      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
      at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:103)
      at org.jboss.seam.intercept.ClientSideInterceptor.invoke(ClientSideInterceptor.java:50)
      at org.javassist.tmp.java.lang.Object_$$_javassist_76.getDefectsForThisRequest(Object_$$_javassist_76.java)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:615)
      at org.jboss.seam.util.Reflections.invoke(Reflections.java:20)
      at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:123)
      at org.jboss.seam.Component.callComponentMethod(Component.java:1834)
      at org.jboss.seam.Component.getInstanceFromFactory(Component.java:1696)
      at org.jboss.seam.Component.getInstance(Component.java:1633)
      at org.jboss.seam.Component.getInstance(Component.java:1610)
      at org.jboss.seam.jsf.SeamVariableResolver.resolveVariable(SeamVariableResolver.java:53)
      at com.sun.facelets.el.LegacyELContext$LegacyELResolver.getValue(LegacyELContext.java:134)
      at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:65)
      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 javax.faces.component.UIData.getValue(UIData.java:1023)
      at javax.faces.component.UIData.createDataModel(UIData.java:789)
      at javax.faces.component.UIData.getDataModel(UIData.java:769)
      at javax.faces.component.UIData.getRowCount(UIData.java:194)
      at org.apache.myfaces.shared_impl.renderkit.html.HtmlTableRendererBase.encodeInnerHtml(HtmlTableRendererBase.java:221)
      at org.apache.myfaces.shared_impl.renderkit.html.HtmlTableRendererBase.encodeChildren(HtmlTableRendererBase.java:134)
      at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:527)
      at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:244)
      at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249)
      at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249)
      at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:573)
      at org.ajax4jsf.framework.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
      at org.ajax4jsf.framework.ajax.AjaxViewHandler.renderView(AjaxViewHandler.java:233)
      at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:41)
      at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:132)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:140)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:63)
      at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
      at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
      at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
      at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
      at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
      at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
      at org.jboss.seam.web.SeamFilter.doFilter(SeamFilter.java:84)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:127)
      at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:277)
      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.Http11Processor.process(Http11Processor.java:869)
      at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
      at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
      at java.lang.Thread.run(Thread.java:797)
      Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ibm.commerce.fixmgmt.model.Request.tracks, 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.PersistentSet.size(PersistentSet.java:139)
      at java.util.ArrayList.(ArrayList.java:158)
      at org.jboss.seam.jsf.SetDataModel.setWrappedData(SetDataModel.java:104)
      at org.jboss.seam.jsf.SetDataModel.(SetDataModel.java:34)
      at org.jboss.seam.databinding.DataModelBinder.wrap(DataModelBinder.java:48)
      at org.jboss.seam.databinding.DataModelBinder.wrap(DataModelBinder.java:19)
      at org.jboss.seam.Component.outjectDataModel(Component.java:1317)
      at org.jboss.seam.Component.outjectDataModels(Component.java:1296)
      at org.jboss.seam.Component.outject(Component.java:1247)
      at org.jboss.seam.interceptors.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:48)
      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
      at org.jboss.seam.interceptors.ManagedEntityIdentityInterceptor.aroundInvoke(ManagedEntityIdentityInterceptor.java:37)
      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
      at org.jboss.seam.interceptors.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:27)
      at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:69)
      at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:103)
      at org.jboss.seam.intercept.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:53)
      at sun.reflect.GeneratedMethodAccessor490.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:615)
      at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
      at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:57)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
      ... 94 more


      Immediately prior to this code being called the 'request' entity is outjected as @DataModelSelection from a @DataTable.

      I can get around the problem by specifying eager loading but I'd rather use the lazy loading. Am I missing something simple or misunderstanding a basic concept? Any help you can provide is really appreciated. Let me know if you need more information about the scenario.

      Thanks very much in advance.