cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to use hibernate transaction in SpringProcessEngineConfiguration?

prathameshj
Champ on-the-rise
Champ on-the-rise
Hello everybody,
I am trying to configure same transaction manager(HibernateTransactionManager)  for Activiti and my database operations.
My goal is whenever exception arises in either Activiti operation or my database operation both transaction should be rolled back.
With the configuration from document I've configured single transaction manager for both but however when I rollback the transaction only my database transaction gets rolled back and Activiti database transaction is not getting rolled back. Posting my code below any kind of inputs will be appreciated.
Note : I am using c3p0 for datasource and HibernateTransactionManager for transaction manager.


Spring configuration XML



<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      <property name="driverClass" value="oracle.jdbc.OracleDriver" />
      <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
      <property name="user" value="activiti" />
      <property name="password" value="activiti" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource">
         <ref bean="dataSource" />
      </property>
      <property name="mappingResources">
         <list>   <value>MyEntity.hbm.xml</value> </list>
      </property>
</bean>

<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref ="sessionFactory" />
      <property name="dataSource" ref="dataSource" />
</bean>
   
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
      <property name="databaseType" value="oracle" />
      <property name="dataSource" ref="dataSource" />
      <property name="transactionManager" ref="hibernateTransactionManager" /> –>
      <property name="transactionsExternallyManaged"  value="true" />
      <property name="databaseSchemaUpdate" value="none" />
      <property name="bulkInsertEnabled" value="false"/>
      <property name="history" value="audit" />
</bean>
   
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
   <property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>   



Java code for performing operations..


[java]
public class MyTestClass{
   @Autowired
   @Qualifier("sessionFactory")
   private SessionFactory sessionFactory;

   @Autowired
   @Qualifier("processEngine")
   private ProcessEngine processEngine = null;

   public void testRollback(HttpServletRequest request, HttpServletResponse response) {
      org.hibernate.Session currentSession = null;
      try {   
         currentSession = sessionFactory.openSession();
         currentSession.setFlushMode(FlushMode.MANUAL);
         
         currentSession.beginTransaction();
         
         //Saving my database object
         MyEntity entity = new MyEntity("My new db record");
         currentSession.save(entity)
         
         //Deploying Workflow
         ZipInputStream inputStream  = new ZipInputStream(Input stream path of zip……..);
            RepositoryService repositoryService = processEngine.getRepositoryService();
            Deployment deployment = repositoryService.createDeployment()
                    .name(version)
                    .addZipInputStream(inputStream)
                    .deploy();
         
         //Exception thrown
         throw new Exception();      
         
         currentSession.flush();
         currentSession.getTransaction().commit();
      } catch (Exception ex) {
            if (currentSession.getTransaction().isActive()) {
               currentSession.getTransaction().rollback();
            }
      }
   }
}
[/java]

In above example MyEntity gets rolled back but Activiti does not roll back deployment of Workflow.
1 REPLY 1

jbarrez
Star Contributor
Star Contributor
No, this won't work: you're starting your transactions manually in hibernate and are not using the transaction manager.
Activiti and Hibernate work together on the transactionmanager level … not on the hibernate level.