3 Replies Latest reply on Jun 7, 2010 1:37 PM by Michael Wohlfart

    JBPM 4.3 + Spring 2.5 + Hibernate: Illegal attempt to associate a collection with two open sessions

    Maninder Singh Newbie

      Hi

      I am trying to integrate BPM 4.3 with our existing application. And I would want the same session factory and the transaction manager to be used by JBPM. For this we have followed the steps mentioned in the Spring integration section in the JBPM docs. My jbpm.cfg.xml looks like this

       

      <jbpm-configuration spring="enabled">

       

        <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.identity.cfg.xml" />

        <import resource="jbpm.tx.spring.cfg.xml" />

      </jbpm-configuration>

       

      And my jbpm.tx.spring.cfg.xml looks like this. Please note we have added the transaction-manager attribute. I am not sure if we need to add

      "<transaction type="spring" />" . Although the documentation says it should be removed.

       

      <process-engine-context>

          <command-service name="newTxRequiredCommandService">

            <retry-interceptor />

            <environment-interceptor policy="requiresNew" />

            <spring-transaction-interceptor policy="requiresNew" transaction-manager="JtaTransactionManager" />

          </command-service>

       

          <!-- Default command service has a Spring transaction interceptor-->

          <command-service name="txRequiredCommandService">

            <retry-interceptor />

            <environment-interceptor />

            <spring-transaction-interceptor  transaction-manager="JtaTransactionManager" />

          </command-service>

       

        </process-engine-context>

       

        <transaction-context>

         <transaction type="spring" />

          <hibernate-session current="true" />

        </transaction-context>

       

      </jbpm-configuration>

       

      My applicationContext has the following entries...

       

      <bean id="JtaTransactionManager" depends-on="userTransactionService">
              <property name="transactionManager" ref="AtomikosTransactionManager" />
              <property name="userTransaction" ref="AtomikosUserTransaction" />
          </bean>

       

      <bean id="sessionFactory">
              <property name="dataSource" ref="atomikosDataSource" />
              <property name="mappingLocations" ref="mappingLocations" />
              <property name="hibernateProperties" ref="hibernateProperties" />
              <!--<property name="jtaTransactionManager" ref="AtomikosTransactionManager" />-->
              <property name="entityInterceptor" ref="auditInterceptor" />
              <property name="lobHandler" ref="lobHandler" />
              <!-- <property name="mappingResources"> 
                    <list> 
                       <value>jbpm.repository.hbm.xml</value> 
                       <value>jbpm.execution.hbm.xml</value> 
                       <value>jbpm.history.hbm.xml</value> 
                       <value>jbpm.task.hbm.xml</value> 
                       <value>jbpm.identity.hbm.xml</value> 
                   </list> 
               </property>--> 
          </bean>
         
          <bean id="springHelper" >
              <property name="jbpmCfg" value="jbpm.ice.cfg.xml"></property>
          </bean> 
         
          <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine"  />

       

       

      ---------------------------------------------------------------------------------

       

      The problem I am getting now is when I run the junit tests. I can see that there are two sessions being created in the database using the same TransactionManager. The exception i get is

       

      Caused by: org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
          at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:432)
          at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:67)
          at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:124)
          at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:121)
          at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:78)
          at org.hibernate.event.def.AbstractSaveEventListener.visitCollectionsBeforeSave(AbstractSaveEventListener.java:394)
          at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:296)
          at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
          at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
          at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
          at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
          at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
          at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
          at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
          at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
          at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
          at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:292)
          at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:240)
          at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
          at org.hibernate.engine.Cascade.cascade(Cascade.java:154)
          at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:454)
          at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
          at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
          at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
          at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
          at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
          at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
          at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
          at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
          at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
          at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
          at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
          at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:697)
          at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
          ... 103 more

       

       

      Any ideas why this might be happening? I am guessing JBPM is creating its own session when it trys to manipulate the domain objects of my application.