3 Replies Latest reply on Oct 19, 2009 8:26 AM by Michael Wohlfart

    Hibernate LazyInitializationException

    Edgar Chen Newbie

      Hello all,

      I have a question in setting Hibernate/jBPM. When I use the api [HistoryTaskInstanceImpl.getHistoryTask], it throws an exception [LazyInitializationException] and shows [could not initialize proxy - no Session]. Anyone knows how to fix it? Really thanks a lot!

      === Environment ==============================
      - jBPM Version : 4.1
      - Database : oracle
      - JDK : 1.6
      - Container : which Java version are you using? use 'java -version' to find out
      - Configuration : using jbpm.cfg.xml only importing files from the jbpm.jar
      lib itself.
      - Libraries : no

      === Process ==================================
      <?xml version="1.0" encoding="UTF-8"?>





      <task candidate-users="mike,peter" form="org/jbpm/examples/taskform/verify_request.ftl" g="96,54,178,52" name="verify_request">







      === API ===================================
      past the relevant code snippet that you use to invoke jBPM

      === Stacktrace ==============================
      Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:498)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      com.gaia.jbpm.action.GetActiveNodeInfo.execute(GetActiveNodeInfo.java:56)
      com.gaia.jbpm.action.GetActiveNodeInfo.doGet(GetActiveNodeInfo.java:33)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      root cause

      org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
      org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
      org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
      org.jbpm.pvm.internal.history.model.HistoryTaskImpl_$$_javassist_18.getId(HistoryTaskImpl_$$_javassist_18.java)
      org.apache.jsp.st_005fview_jsp._jspService(st_005fview_jsp.java:162)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      com.gaia.jbpm.action.GetActiveNodeInfo.execute(GetActiveNodeInfo.java:56)
      com.gaia.jbpm.action.GetActiveNodeInfo.doGet(GetActiveNodeInfo.java:33)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

        • 1. Re: Hibernate LazyInitializationException
          Sebastian Schneider Master

          Your process definition got lost. Can you repost it using the code-tags? Also please provide a unit test to show how you invoke the API.

          • 2. Re: Hibernate LazyInitializationException
            Edgar Chen Newbie

             

            "sebastian.s" wrote:
            Your process definition got lost. Can you repost it using the code-tags? Also please provide a unit test to show how you invoke the API.


            Thanks Sebastian. Infos below:

            process definition : (Same as examples/taskform)
            <?xml version="1.0" encoding="UTF-8"?>
            
            <process name="VacationRequest" xmlns="http://jbpm.org/4.0/jpdl">
             <start form="org/jbpm/examples/taskform/request_vacation.ftl" g="16,56,48,48" name="start">
             <transition to="verify_request"/>
             </start>
             <task candidate-users="mike,peter" form="org/jbpm/examples/taskform/verify_request.ftl" g="96,54,178,52" name="verify_request">
             <transition g="-34,9" name="reject" to="vacation_rejected"/>
             <transition g="-46,-26" name="accept" to="vacation_accepted"/>
             </task>
             <end g="306,16,48,48" name="vacation_accepted"/>
             <end g="306,96,48,48" name="vacation_rejected"/>
            </process>
            


            I am trying to get all history information of an process instance using Servlet and JSP. Codes below :

            ProcessManager:
            public ProcessInstaticeActivityInfo getActivityCoordinatesInfo(String processInstanceId) {
             ProcessDefinitionImpl processDefinition = (ProcessDefinitionImpl) this
             .getProcessDefinitionByProcessInstanceId(processInstanceId);
             String pdId = processDefinition.getId();
             List<ActivityInfo> activeCoordinates = new ArrayList<ActivityInfo>();
            
             List<ActivityInfo> coordinateList = new ArrayList<ActivityInfo>();
             {
             ActivityInfo activityInfo = new ActivityInfo(processDefinition.getInitial().getCoordinates());
             coordinateList.add(activityInfo);
             List<HistoryActivityInstance> activities = historyService.createHistoryActivityInstanceQuery()
             .executionId(processInstanceId).list();
             for (HistoryActivityInstance activity : activities) {
             String activityName = activity.getActivityName();
             activityInfo = new ActivityInfo(repositoryService.getActivityCoordinates(pdId, activityName));
             activityInfo.setActivityInstance(activity);
             coordinateList.add(activityInfo);
             }
             }
            
             HistoryProcessInstance hpi = getHistoryProcessInstancByProcessInstanceId(processInstanceId);
             String endActivityName = ((HistoryProcessInstanceImpl) hpi).getEndActivityName();
            
             if (endActivityName != null) {
             ActivityCoordinates coordinates = repositoryService.getActivityCoordinates(pdId, endActivityName);
             activeCoordinates.add(new ActivityInfo(coordinates));
             } else {
             Set<String> activityNames = executionService.findExecutionById(processInstanceId)
             .findActiveActivityNames();
             for (String activityName : activityNames) {
             ActivityCoordinates coordinates = repositoryService.getActivityCoordinates(pdId, activityName);
             activeCoordinates.add(new ActivityInfo(coordinates));
             }
             }
             return new ProcessInstaticeActivityInfo(activeCoordinates, coordinateList);
             }
            


            Servlet:
            public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
             {
             ProcessManager pm = new ProcessManager();
             String taskId =
             request.getParameter("taskid");
             String piId = request.getParameter("pid");
             ProcessInstaticeActivityInfo processInstaticeActivityInfo = pm.getActivityCoordinatesInfo(piId);
             request.setAttribute("activeActivityInfos", processInstaticeActivityInfo.getActiveActivityInfos());
             request.setAttribute("processedActivityInfos", processInstaticeActivityInfo.getProcessedActivityInfos());
             request.setAttribute("pm",pm);
             response.setContentType("text/html;charset=UTF-8");
             RequestDispatcher dispatcher = request.getRequestDispatcher("st_view.jsp");
             dispatcher.forward(request, response);
             }
            


            jsp:
            ProcessManager pm = (ProcessManager)request.getAttribute("pm");
             int id = 0;
             List<ActivityInfo> processedActivityInfos = (List<ActivityInfo>)request.getAttribute("processedActivityInfos"); ;
             List<ActivityInfo> activeActivityInfos = (List<ActivityInfo>)request.getAttribute("activeActivityInfos");
             ActivityCoordinates coordinates;
             HistoryActivityInstance activityInstance;
             for(ActivityInfo info : processedActivityInfos){
             coordinates = info.getCoordinates();
             activityInstance = info.getActivityInstance();
             String elId = "_pai_" + (++id);
             String title = activityInstance==null?"":activityInstance.getActivityName();
             StringBuffer position = new StringBuffer();
             position.append("left:").append(coordinates.getX()+2).append("px;");
             position.append("top:").append(coordinates.getY()+2).append("px;");
             position.append("height:").append(coordinates.getHeight()-10).append("px;");
             position.append("width:").append(coordinates.getWidth()-10).append("px;");
             StringBuffer text = new StringBuffer();
            
             if (activityInstance instanceof HistoryDecisionInstanceImpl) {
             text.append("<b>CompleteTime:</b> ").append(activityInstance.getEndTime());
             String transitionName = ((HistoryActivityInstanceImpl) activityInstance).getTransitionName();
             if("completed".equals(transitionName)){
             ProcessDefinition pd = pm.getProcessDefinitionByProcessInstanceId(request.getParameter("piId"));
             if(pd!=null){
             Activity activity = ((ProcessDefinitionImpl)pd).getActivitiesMap().get(activityInstance.getActivityName());
             if(activity!=null){
             text.append("<br/><b>Transaction:</b> ").append(activity.getDefaultOutgoingTransition().getName());
             }
             }
             } else {
             text.append("<br/><b>Transaction:</b> ").append(transitionName);
             }
             } else if (activityInstance instanceof HistoryTaskInstanceImpl) {
             HistoryTaskInstanceImpl taskInstanceImpl = (HistoryTaskInstanceImpl) activityInstance;
            
            
             text.append("<b>Assignee</b>: ").append(taskInstanceImpl.getHistoryTask().getAssignee());
             text.append("<br/><b>Complete Time:</b> ").append(activityInstance.getEndTime());
             String transitionName = ((HistoryActivityInstanceImpl) activityInstance).getTransitionName();
             if("completed".equals(transitionName)){
             ProcessDefinition pd = pm.getProcessDefinitionByProcessInstanceId(request.getParameter("piId"));
             if(pd!=null){
             Activity activity = ((ProcessDefinitionImpl)pd).getActivitiesMap().get(activityInstance.getActivityName());
             if(activity!=null){
             text.append("<br/><b>Transaction:</b> ").append(activity.getDefaultOutgoingTransition().getName());
             }
             }
             } else {
             text.append("<br/><b>Transaction:</b> ").append(transitionName);
             }
            
             List<HistoryComment> comments = pm.getTaskComments(taskInstanceImpl.getHistoryTask().getId());
             if(comments.size()>0){ // 表示最新的comment
             text.append("<br/><br/><b>Comments:</b> ").append(comments.get(comments.size()-1).getMessage());
             }
             }
            


            • 3. Re: Hibernate LazyInitializationException
              Michael Wohlfart Expert

              to me this looks like a configuration issue in your jbpm-config file, check your transaction-context tag , for me the following setup works:

               <transaction-context>
               <transaction />
               <repository-session />
               <db-session />
               <message-session />
               <timer-session />
               <history-session />
               <hibernate-session />
               </transaction-context>
              

              but maybe you want to set:
               <hibernate-session current="true" />
              

              or maybe you forgot the
               <history-service />
              

              in the jbpm-config file?