2 Replies Latest reply on Oct 13, 2010 4:32 AM by Anup Nair

    Hibernate LazyInitializationException during swimlane creation

    Anup Nair Newbie

      Hi All,

       

      I am a new bie to this site. I am currently using jBPM 4 with Oracle as db.

       

      I have created a sample workflow and using the JBPM API to create instances and moving instance from one activity to another.

       

      However when i am trying to create a swimlane programatically i am getting org.hibernate.LazyInitializationException

       

      I am not getting an idea how the hibernate session gets closed.

       

      Following is the code snippet for swimlane creation:

       

      public void testAddParticipants() throws Exception{
              ProcessInstance processInstance = executionService.findProcessInstanceById("ServiceCenter.1");
              EnvironmentImpl    environment =(EnvironmentImpl)((EnvironmentFactory)processService).openEnvironment();
              try{
                  Collection<? extends Execution> executionMap = processInstance.getExecutions();
                  for (Execution execution : executionMap) {
                      System.out.println(execution.getClass());
                      if(execution instanceof ExecutionImpl){
                          final ExecutionImpl exec = (ExecutionImpl) execution;
                          SwimlaneImpl role = exec.createSwimlane("approver");
                          System.out.println("Approver role added:"+role);
                      }

       

                            }
              }finally{
                  environment.close();
              }
          }

       

       

      The exception trace is:

       

      ### EXCEPTION ###########################################
      [LazyInitializationException] failed to lazily initialize a collection of role: org.jbpm.pvm.internal.model.ExecutionImpl.swimlanes, no session or session was closed
      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.jbpm.pvm.internal.model.ExecutionImpl.swimlanes, no session or session was closed

          at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
          at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
          at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
          at org.hibernate.collection.PersistentMap.put(PersistentMap.java:184)
          at org.jbpm.pvm.internal.model.ExecutionImpl.createSwimlane(ExecutionImpl.java:874)
          at com.jpmc.servicecenter.ServiceCenter.testAddParticipants(ServiceCenter.java:304)
          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:597)
          at junit.framework.TestCase.runTest(TestCase.java:164)
          at junit.framework.TestCase.runBare(TestCase.java:130)
          at junit.framework.TestResult$1.protect(TestResult.java:106)
          at junit.framework.TestResult.runProtected(TestResult.java:124)
          at junit.framework.TestResult.run(TestResult.java:109)
          at junit.framework.TestCase.run(TestCase.java:120)
          at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
          at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
          at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

       

      The jbpm.cfg.xml has the following configuration:

       

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

       

      <jbpm-configuration>

       

        <import resource="jbpm.default.cfg.xml" />
        <import resource="jbpm.businesscalendar.cfg.xml" />
        <import resource="jbpm.tx.hibernate.cfg.xml" />
        <import resource="jbpm.jpdl.cfg.xml" />
        <import resource="jbpm.bpmn.cfg.xml" />
        <import resource="jbpm.identity.cfg.xml" />
        <import resource="jbpm.task.hbm.xml" />

       

        <!-- Job executor is excluded for running the example test cases. -->
        <!-- To enable timers and messages in production use, this should be included. -->
        <!--
        <import resource="jbpm.jobexecutor.cfg.xml" />
        -->

       

      </jbpm-configuration>

       

      I am stuck with this error from the past 1 week.

       

      please help me out.

       

      Thanks in advance.

        • 1. Re: Hibernate LazyInitializationException during swimlane creation
          Michael Wohlfart Expert

          The hibernate session is managed the way you configure it with <hibernate-session-factory /> or <hibernate-session />, the config for this is probably in your jbpm.tx.hibernate.cfg.xml file.

          Did you try using a command like described by HuiSheng in this thread: http://community.jboss.org/message/536708 ?

          • 2. Re: Hibernate LazyInitializationException during swimlane creation
            Anup Nair Newbie

            Hi Micheal,

             

            I tried using the command as described above.

             

            Here is my code snippet:

             

            final ProcessInstance processInstance = executionService.findProcessInstanceById("ServiceCenter.1");
                    EnvironmentImpl    environment =(EnvironmentImpl)((EnvironmentFactory)processService).openEnvironment();
                    try{
                                         
                                processService.execute(new Command() {

             

                                    public Object execute(Environment environment)
                                            throws Exception {
                                        Session session = environment.get(Session.class);
                                        Transaction tx  = session.beginTransaction();
                                        SwimlaneImpl role = ((ExecutionImpl)processInstance).createSwimlane("approver");
                                        session.update(processInstance);
                                        tx.commit();
                                        System.out.println("Approver role added:"+role);
                                        return role;
                                    }
                                });

             

                    }finally{
                        environment.close();
                    }

             

            The ExecutionImpl class has swimlanes map to which this newly created swimlane is inserted. However the map is not lazily getting initialized since the session gets closed somehow.

             

             

            My jbpm.tx.hibernate.cfg.xml has following configuration:

             

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

             

            <jbpm-configuration>

             

              <process-engine-context>

             

                <command-service name="txRequiredCommandService">
                  <skip-interceptor />
                  <retry-interceptor />
                  <environment-interceptor />
                  <standard-transaction-interceptor />
                </command-service>

             

                <command-service name="newTxRequiredCommandService">
                  <retry-interceptor />
                  <environment-interceptor policy="requiresNew" />
                  <standard-transaction-interceptor />
                </command-service>

             

                <hibernate-configuration>
                  <cfg resource="jbpm.hibernate.cfg.xml" />    
                </hibernate-configuration>

             

                <hibernate-session-factory />
               
              </process-engine-context>

             

              <transaction-context>
                <transaction />
                <hibernate-session />
              </transaction-context>

             

            Please tell me is there anything else to be added to this configuration.

             

            Thakns in advance.