AnsweredAssumed Answered

How do two service method TaskService in a one transaction?

Question asked by kolmogorov.dikma on Mar 28, 2013
Latest reply on Apr 1, 2013 by udoderk
I wrote a simple test that calls a service that creates a task and stores variables. Between conservation task and variables raises an exception, the task saved, and the variables do not. What do I need to call these methods executed in a single transaction?

junit test

        @Test
   @Rollback(value = false)
   public void testTaskWorkFlowServiceTran() throws Exception {

      Map<String, Object> vars = new HashMap<String, Object>();
      vars.put("var1", "value1");

      TaskEntity task = new TaskEntity();
      task.setName("name");
      task.setDescription("desc");
      task.setPriority(60);
      task.setDueDate(Calendar.getInstance().getTime());
      task.setOwner("owner");
      task.setAssignee("assignee");
      taskTestService.createTask(task, vars);
   }

spring service

@Service
@Transactional
public class TaskTestService {

   @Autowired
   private TaskService taskService;
   
   public void createTask(TaskEntity taskEntity, Map<String, Object> vars) throws Exception {
      Task task = taskService.newTask();
      task.setName(taskEntity.getName());
      task.setDescription(taskEntity.getDescription());
      task.setPriority(taskEntity.getPriority());
      task.setDueDate(taskEntity.getDueDate());
      task.setOwner(taskEntity.getOwner());
      task.setAssignee(taskEntity.getAssignee());
      taskService.saveTask(task);
      
      if (vars != null) {
         throw new Exception("boom!");
      } else {
         taskService.setVariables(task.getId(), vars);
      }
   }
}

application context

       <context:component-scan base-package="ru.nti.siu.bpm.test" />
   
   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
      <property name="databaseType" value="postgres" />
      <property name="dataSource" ref="myDataSource" />
      <property name="transactionManager" ref="myTransactionManager" />
      <property name="databaseSchemaUpdate" value="false" />
      <property name="history" value="full" />
      <property name="jobExecutorActivate" value="false" />
      <property name="idGenerator">
         <bean class="org.activiti.engine.impl.persistence.StrongUuidGenerator" />
      </property>
   </bean>

   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
      <property name="processEngineConfiguration" ref="processEngineConfiguration" />
   </bean>

   <bean id="repositoryService" factory-bean="processEngine"
      factory-method="getRepositoryService" />
   <bean id="runtimeService" factory-bean="processEngine"
      factory-method="getRuntimeService" />
   <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="identityService" factory-bean="processEngine"
      factory-method="getIdentityService" />
      
   <bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="myDataSource" />
     </bean>
       
    <bean id="myDataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
       <property name="driverClass" value="org.postgresql.Driver" />
       <property name="url" value="jdbc:postgresql://192.168.1.1/mydatabase" />
       <property name="username" value="postgres" />
       <property name="password" value="postgres" />
     </bean>

Outcomes