2 Replies Latest reply on Dec 12, 2010 12:51 PM by mqqla2

    Spring 3 + jBPM 4.4 - 2 different hibernate sessions

    mqqla2 Newbie

      Hi,

       

      I've strange problem with spring and jbpm. I have code which works sth like that:

      Session session = sessionFactory.getCurrentSession();

      Order order = new Order();

      ExecutionImpl e = processEngine.startProcess(order);

      log(e.getId());   //this logs new process id from jbpm4_execution table, let's say id = 1000

      order.setProcess(e);

      session.save(order);

       

      order entity has a foreign key to the process it's connected to.

       

      on saving order in last line I have error from DB the process with id = 1000 does not exist.

       

      I debug jbpm and dig into RepositorySessionImpl - it has hibernate session object encapsulated and it's different than 'my' session object.

      But they both have the same SessionFactory object (the same hashcode etc).

       

      My spring context xml looks like that (fragment)

       

      <bean id="sessionFactory" autowire="default"

      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

      <property name="dataSource" ref="dataSource" />

      <property name="hibernateProperties">

      <props>

      <prop key="hibernate.format_sql">true</prop>

      <prop key="hibernate.show_sql">true</prop>

      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>

       

      </props>

      </property>

      <property name="configLocation" value="classpath:jbpm.hibernate.cfg.xml" />

      <property name="mappingLocations">

      <list>

      <value>classpath:jbpm.execution.hbm.xml</value>

      <value>classpath:jbpm.repository.hbm.xml</value>

      <value>classpath:jbpm.task.hbm.xml</value>

      <value>classpath:jbpm.history.hbm.xml</value>

      <value>classpath:jbpm.identity.hbm.xml</value>

      </list>

      </property>

       

      <property name="packagesToScan">

      <list>

      <value>entities.to.scan</value>

      </list>

      </property>

      </bean>

       

      <bean id="dataSource"

      class="org.springframework.jdbc.datasource.DriverManagerDataSource">

       

      <property name="driverClassName" value="org.hibernate.dialect.PostgreSQLDialect" />

      <property name="url" value="jdbc:postgresql://192.168.15.30/jbpm_test" />

      <property name="username" value="jbpm" />

      <property name="password" value="jbpm" />

      </bean>

       

      <!-- enable the configuration of transactional behavior based on annotations -->

      <tx:annotation-driven transaction-manager="txManager" />

       

      <bean id="txManager"

      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       

      <property name="dataSource" ref="dataSource" />

      </bean>

      <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper">

      <property name="jbpmCfg">

      <value>jbpm.cfg.xml</value>

      </property>

      </bean>

      <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper">
      <property name="jbpmCfg">
      <value>jbpm.cfg.xml</value>
      </property>
      </bean>
      <bean id="processEngine" factory-bean="springHelper"
      factory-method="createProcessEngine" />
      <bean id="repositoryService" factory-bean="processEngine"
      factory-method="getRepositoryService" />
      <bean id="executionService" factory-bean="processEngine"
      factory-method="getExecutionService" />
      <bean id="taskService" factory-bean="processEngine"
      factory-method="getTaskService" />
      <bean id="historyService" factory-bean="processEngine"
      factory-method="getHistoryService" />
      <bean id="managementService" factory-bean="processEngine"
      factory-method="getManagementService" />

      <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" />

      <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />

      <bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />

      <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />

      <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />

      <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

       

      and jbpm.cfg.xml looks like that:

       

      <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" />

       

        <!-- 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" />

        -->

       

        <process-engine-context>

      <command-service name="newTxRequiredCommandService">

      <retry-interceptor />

      <environment-interceptor policy="requiresNew" />

      <spring-transaction-interceptor

      policy="requiresNew"  />

      </command-service>

       

      <command-service name="txRequiredCommandService">

      <retry-interceptor />

      <environment-interceptor />

      <spring-transaction-interceptor />

      </command-service>

      </process-engine-context>

       

      <transaction-context>

              <transaction type="spring" />

              <hibernate-session current="true" />

          </transaction-context>

       

      </jbpm-configuration>

      Where's my mistake? It looks there is no shared transaction between my code and jbpm code. All methods are marked @Transactional in my code, and stack trace shows tx interceptors is up and running. But I still don't know why I have 2 different sessions and 1 common sessionfactory.
      any help would be appreciated
      regards
      mk

        • 1. Re: Spring 3 + jBPM 4.4 - 2 different hibernate sessions
          Michael Wohlfart Expert

          Hi mqqla2,

          my jbpm.cfg.xml for spring looks a bit different:

           

          <jbpm-configuration spring="enabled">
            [...]
            <command-service name="newTxRequiredCommandService">
              <retry-interceptor />
              <environment-interceptor policy="requiresNew" />
              <spring-transaction-interceptor transaction-manager="localTransactionManager" current="false" policy="requiresNew" />
            </command-service>
            [...]
            <command-service name="txRequiredCommandService">
              <retry-interceptor />
              <environment-interceptor />
              <spring-transaction-interceptor transaction-manager="localTransactionManager" current="true" />
            </command-service>
            [...]
            <transaction-context>
              <transaction type="spring"/>
              [...]
              <hibernate-session factory="sessionFactory" current="true" close="false" tx="true" />
            </transaction-context>
          </jbpm-configuration>
          
          

           

          "sessionFactory" and "localTransactionManager" are spring beans, HTH

          • 2. Re: Spring 3 + jBPM 4.4 - 2 different hibernate sessions
            mqqla2 Newbie

            Thanks for reply. I just had my config doubled. One file had current="true" set in hibernate-session, another had that attribute not set. And was load first so misconfig problem. Anyway eclipse debugger rulez

             

            regards

            m