AnsweredAssumed Answered

Spring JpaTransactionManager not saving Activiti entities to DB

Question asked by neville.sequeira on Sep 11, 2013
Latest reply on Sep 18, 2013 by neville.sequeira
Nothing about Activiti is being saved to database. Application entities ARE being saved to database.  Below, in order are, the spring file, persitence.xml file and the test case.

Sprint file:

   <context:annotation-config/>
   <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>
   <tx:annotation-driven transaction-manager="transactionManager"/>

   
   <bean id="ActivitiTrialDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
      <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
      <property name="url" value="jdbc:jtds:sqlserver://localhost:1433/ActivitiTrial" />
      <property name="username" value="ActivitiTrial" />
      <property name="password" value="ActivitiTrial" />
      <property name="defaultAutoCommit" value="false" />
      <property name="initialSize" value="5" />
   </bean>


   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="ActivitiTrialDataSource" />
       <property name="persistenceUnitName" value="ActivitiTrial"/>
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
       <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>



   <!– Activiti –>
   <bean id="activitiDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
      <property name="targetDataSource" ref="ActivitiTrialDataSource" />
   </bean>
   
   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
      <property name="databaseType" value="mssql" />
       <property name="dataSource" ref="activitiDataSource" />
      <property name="transactionsExternallyManaged" value="true" />
       <property name="transactionManager" ref="transactionManager" />
       <property name="databaseSchemaUpdate" value="false" />
      <property name="history" value="audit" />
       <property name="jobExecutorActivate" value="false" />
   </bean>
 
     <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
      <property name="processEngineConfiguration" ref="processEngineConfiguration" />
   </bean>

   <bean id="activitiRepositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
   <bean id="activitiRuntimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
   <bean id="activitiTaskService" factory-bean="processEngine" factory-method="getTaskService" />
   <bean id="activitiHistoryService" factory-bean="processEngine" factory-method="getHistoryService" />
   <bean id="activitiManagementService" factory-bean="processEngine" factory-method="getManagementService" />



Persistence.xml file:

<persistence-unit name="ActivitiTrial">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
       
        <properties>
            <property name="hibernate.archive.autodetection" value="hbm,class"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
            <property name="hibernate.hbm2ddl.auto" value="none"/>
         <property name="hibernate.show_sql" value="false"/>
         <property name="hibernate.ejb.metamodel.generation" value="disabled"/>
        </properties>
</persistence-unit>



TestClass :

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback=false)
@ContextConfiguration({"classpath:/springApplicationContext.xml"})
public class TrialTest {
   @Autowired
   RepositoryService activitiRepositoryService;

   @Autowired
   TaskService activitiTaskService;


   /**
    * Used for persistence of entities not related to Activiti process engine. i.e, application entities
    * Activiti process engine related persistence is as per a configuration bean named "processEngineConfiguration" found in the Activiti
         * configuration file activiti.cfg.xml.
    */
   @PersistenceContext(unitName="ActivitiTrial")
   EntityManager entityManager;

   
   @Autowired
   ProcessService<String> processServiceActiviti;

   
   @Test
   @Transactional
   public void trialTest() throws Exception {
      activitiRepositoryService.createDeployment().addClasspathResource("process-definitions/neville.bpmn20.xml").deploy();

      
      // 1. Do application work. VERIFIED - THIS ENTITY IS BEING SAVED TO DB.
      ApplicationEntity applicationEntity1 = new ApplicationEntity();
      applicationEntity1.name = "App entity 1";
      entityManager.persist(applicationEntity1);

      
      // 2. Do Actviti work. VERIFIED - None of the ACTIVITI objects are saved to the db.
      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("aProcessWithOneTaskBetweenStartAndEnd");
      String processInstanceId = processInstance.getId();
      Task userTask = activitiTaskService.createTaskQuery().processInstanceId(processInstanceId).list().get(0);
                // VERIFIED that userTask is not null.

      
      // 1. Do some more application work. VERIFIED - THIS ENTITY IS BEING SAVED TO DB.
      ApplicationEntity applicationEntity2 = new ApplicationEntity();
      applicationEntity2.name = "App entity 2";
      entityManager.persist(applicationEntity2);
   }
}

Outcomes