jBPM 6.1 doesn't work properly with JTA transactions
d.losev Jun 17, 2014 9:42 PMI am using JBPM 6.1 and trying to integrate it with my Spring app. The application is deployed on JBoss 6.2 EAP. I followed instructions from this post http://planet.jboss.org/post/jbpm_6_with_spring and successfully integrated. But I noticed the strange behavior when tasks are executed in separate transactions.
I thought my configuration was wrong. But then I found this test https://github.com/droolsjbpm/droolsjbpm-integration/blob/master/kie-spring/src/test/java/org/kie/spring/jbpm/JTAEntityManagerFactorySpringTest.java that correctly works with "sample.bpmn". I modified this test to work with my process and i got exactly the same issue.
My business process diagram is:
I'm trying to start new process and execute "Create" task in one transaction. Then I'm trying to execute a few "Edit" tasks in separate transactions. My code is below. I've just modified the test's code to work with my process:
@Test public void testSpringWithJTAAndEMFwithRollback() throws Exception{ context = new ClassPathXmlApplicationContext("jbpm/jta-emf/jta-emf-spring.xml"); RuntimeManager manager = (RuntimeManager) context.getBean("runtimeManager"); RuntimeEngine engine = manager.getRuntimeEngine(null); KieSession ksession = engine.getKieSession(); InternalTaskService taskService = (InternalTaskService) engine.getTaskService(); UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("owner", "john"); ProcessInstance processInstance = ksession.startProcess("expense", parameters); TaskSummary task = getTasksByProcessByTaskName(processInstance.getId(), "create", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); ut.commit(); ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); task = getTasksByProcessByTaskName(processInstance.getId(), "edit", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); ut.commit(); ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); task = getTasksByProcessByTaskName(processInstance.getId(), "edit", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); ut.commit(); ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); task = getTasksByProcessByTaskName(processInstance.getId(), "edit", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); ut.commit(); } private TaskSummary getTasksByProcessByTaskName(long processId, String taskName, InternalTaskService tss) { Map<String, List<?>> parameters = new HashMap<String, List<?>>(); parameters.put(TaskQueryService.PROCESS_INST_ID_LIST, Arrays.asList(processId)); parameters.put(TaskQueryService.STATUS_LIST, Arrays.asList(Status.Ready, Status.Created, Status.Reserved)); List<TaskSummary> tasks = tss.getTasksByVariousFields(parameters, false); for (TaskSummary t : tasks) { if (t.getName().equalsIgnoreCase(taskName)) { return t; } } return null; }
The problem happens when "Edit" task is executed for the second time. In that moment there is no "Edit" task with any status. There are only "Delete" and "Submit" tasks available.
But this problem doesn't happen if all the tasks are executed in one transaction. The following code works as expected:
@Test public void testSpringWithJTAAndEMFwithRollback() throws Exception{ context = new ClassPathXmlApplicationContext("jbpm/jta-emf/jta-emf-spring.xml"); RuntimeManager manager = (RuntimeManager) context.getBean("runtimeManager"); RuntimeEngine engine = manager.getRuntimeEngine(null); KieSession ksession = engine.getKieSession(); InternalTaskService taskService = (InternalTaskService) engine.getTaskService(); UserTransaction ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction"); ut.begin(); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("owner", "john"); ProcessInstance processInstance = ksession.startProcess("expense", parameters); TaskSummary task = getTasksByProcessByTaskName(processInstance.getId(), "create", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); task = getTasksByProcessByTaskName(processInstance.getId(), "edit", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); task = getTasksByProcessByTaskName(processInstance.getId(), "edit", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); task = getTasksByProcessByTaskName(processInstance.getId(), "edit", taskService); taskService.start(task.getId(), "john"); taskService.complete(task.getId(), "john", null); ut.commit(); }
What is wrong here? How to make jBPM execute tasks in separate transactions?
-
expense.bpmn2.zip 2.8 KB