5 Replies Latest reply on Mar 16, 2017 5:14 AM by swiderski.maciej

    jBPM 6.1 doesn't work properly with JTA transactions

    d.losev

      I 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:

       

      jbpm_6.png

       

      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?

        • 1. Re: jBPM 6.1 doesn't work properly with JTA transactions
          swiderski.maciej

          this certainly should work as you described, so please open a jira for it and would be best if you could create pull request with added test case that illustrates the issue for droolsjbpm-integration module so I could much quicker get into it and try to resolve the case. Please assign the jira to me directly so I won't miss it.

           

          HTH

          • 2. Re: jBPM 6.1 doesn't work properly with JTA transactions
            d.losev

            Maciej, thank you for your help.

             

            I cloned the latest version of droolsjbpm-integration repository. The master branch is on 49f3169162973162ef2267e49e3a8693ce382f50 "Merge pull request #84 from vinodkiran/spring-xsd-changes" commit.

            Then I modified JTAEntityManagerFactorySpringTest code to work with my business process and run the test. But now I don't see any problems with transactions. The problem has gone.

             

            Then I tried on previously cloned  repository again (cloned previous weekend). The master branch is on c3f7923979d1b66fd4f3b7d101f7f3733e956b1d "BZ-1095980: preventing NPE at runtime". And I could see the issue. It seems jBPM sources have been changed since last weekend.

             

            Now that issue doesn't happen in 6.2.0-SNAPSHOT and 6.1.0-SNAPSHOT. But It still happens in 6.1.0.CR1. So, I guess I need to wait for 6.1.0.CR2 to get working version.

             

            Anyway, here is  my pool request https://github.com/droolsjbpm/droolsjbpm-integration/pull/85 where I show that the issue really happens on 6.1.0.CR1.

            • 3. Re: jBPM 6.1 doesn't work properly with JTA transactions
              swiderski.maciej

              Thanks Dmitry, I'll check that up and merge your test case so we have it covered with default test suite during the build.

               

              HTH

              • 4. Re: jBPM 6.1 doesn't work properly with JTA transactions
                nishantkparmar

                Maciej,

                 

                Is this transaction issue resolved in 6.1.0-Final version or we need to switch to 6.2.0.0?

                 

                We are using jbpm 6.1.0-Final and using in our Spring based application:

                During Complete task we are getting below transaction exception. Issue is sporadic but in integrated env it appears a lot like every 1 in 10 transaction.

                 

                [TaskTransactionInterceptor.java:86] org.jbpm.services.task.persistence.TaskTransactionInterceptor| Could not rollback

                java.lang.RuntimeException: Unable to rollback transaction

                               at org.kie.spring.persistence.KieSpringTransactionManager.rollback(KieSpringTransactionManager.java:90) ~[kie-spring-6.1.0.Final.jar:6.1.0.Final]

                               at org.jbpm.services.task.persistence.TaskTransactionInterceptor.rollbackTransaction(TaskTransactionInterceptor.java:84) [jbpm-human-task-jpa-6.1.0.Final.jar:6.1.0.Final]

                               at org.jbpm.services.task.persistence.TaskTransactionInterceptor.execute(TaskTransactionInterceptor.java:72) [jbpm-human-task-jpa-6.1.0.Final.jar:6.1.0.Final]

                               at org.jbpm.services.task.commands.TaskCommandExecutorImpl.execute(TaskCommandExecutorImpl.java:40) [jbpm-human-task-core-6.1.0.Final.jar:6.1.0.Final]

                               at org.jbpm.services.task.impl.command.CommandBasedTaskService.complete(CommandBasedTaskService.java:144) [jbpm-human-task-core-6.1.0.Final.jar:6.1.0.Final]

                               at org.jbpm.runtime.manager.impl.task.SynchronizedTaskService.complete(SynchronizedTaskService.java:129)

                Caused by: org.springframework.transaction.IllegalTransactionStateException: Transaction is already completed - do not call commit or rollback more than once per transaction

                               at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:825) ~[spring-tx-4.2.5.RELEASE.jar:4.2.5.RELEASE]

                 

                Do you think upgrading to next version like 6.2.0 will help?

                • 5. Re: jBPM 6.1 doesn't work properly with JTA transactions
                  swiderski.maciej

                  I would recommend to upgrade to latest (6.5.0.Final) version as since 6.1 there was a lot of fixes and improvements.