OptimisticLockException in multi-instance sub-process

Hello everyone,

I have created a simple flow, containing subprocess, service tasks, and inner multi-instance sub-process.
The AsyncJobExecutor is enabled, the History is disabled.
Some of the service tasks are asynchronous and not exclusive(for better performance). The service tasks, inside the multi-instance sub-process, are async and exclusive. When I start the process, a FlowableOptimisticLockException is thrown by the engine. What have I done wrong? How can this exception be avoided, without changing the process definition structure? (this test example is an isolated part of a bigger productive process)

  • Process definition:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="testProcess" name="Test process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <subProcess id="outerSubProcess" name="Outer Sub Process">
      <subProcess id="multiInstanceSubProcess" name="Multi Instance Sub Process">
        <multiInstanceLoopCharacteristics isSequential="true" activiti:collection="testCollection" activiti:elementVariable="testVariable"></multiInstanceLoopCharacteristics>
        <startEvent id="startevent3" name="Start"></startEvent>
        <serviceTask id="secondInnerDelegate" name="Second Inner Delegate" activiti:async="true" activiti:class="SecondInnerDelegate"></serviceTask>
        <endEvent id="endevent3" name="End"></endEvent>
        <serviceTask id="firstInnerDelegate" name="First Inner Delegate" activiti:async="true" activiti:class="FirstInnerDelegate"></serviceTask>
        <sequenceFlow id="flow21" sourceRef="secondInnerDelegate" targetRef="endevent3"></sequenceFlow>
        <sequenceFlow id="flow22" sourceRef="firstInnerDelegate" targetRef="secondInnerDelegate"></sequenceFlow>
        <sequenceFlow id="flow23" sourceRef="startevent3" targetRef="firstInnerDelegate"></sequenceFlow>
      </subProcess>
      <endEvent id="endevent2" name="End"></endEvent>
      <startEvent id="startevent4" name="Start"></startEvent>
      <serviceTask id="firstOuterDelegate" name="First Outer Delegate" activiti:class="FirstOuterDelegate"></serviceTask>
      <sequenceFlow id="flow25" sourceRef="startevent4" targetRef="firstOuterDelegate"></sequenceFlow>
      <serviceTask id="secondOuterDelegate" name="Second Outer Delegate" activiti:async="true" activiti:exclusive="false" activiti:class="SecondOuterDelegate"></serviceTask>
      <sequenceFlow id="flow27" sourceRef="firstOuterDelegate" targetRef="secondOuterDelegate"></sequenceFlow>
      <serviceTask id="thirdOuterDelegate" name="Third Outer Delegate" activiti:async="true" activiti:exclusive="false" activiti:class="ThirdOuterDelegate"></serviceTask>
      <sequenceFlow id="flow28" sourceRef="multiInstanceSubProcess" targetRef="thirdOuterDelegate"></sequenceFlow>
      <sequenceFlow id="flow29" sourceRef="thirdOuterDelegate" targetRef="endevent2"></sequenceFlow>
      <sequenceFlow id="flow30" sourceRef="secondOuterDelegate" targetRef="multiInstanceSubProcess"></sequenceFlow>
    </subProcess>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="outerSubProcess" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="startevent1" targetRef="outerSubProcess"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_testProcess">
    <bpmndi:BPMNPlane bpmnElement="testProcess" id="BPMNPlane_testProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="41.0" width="41.0" x="171.0" y="355.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="outerSubProcess" id="BPMNShape_outerSubProcess">
        <omgdc:Bounds height="331.0" width="856.0" x="275.0" y="210.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="multiInstanceSubProcess" id="BPMNShape_multiInstanceSubProcess">
        <omgdc:Bounds height="158.0" width="271.0" x="660.0" y="300.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="startevent3" id="BPMNShape_startevent3">
        <omgdc:Bounds height="35.0" width="35.0" x="670.0" y="329.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="secondInnerDelegate" id="BPMNShape_secondInnerDelegate">
        <omgdc:Bounds height="55.0" width="105.0" x="680.0" y="380.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent3" id="BPMNShape_endevent3">
        <omgdc:Bounds height="35.0" width="35.0" x="870.0" y="390.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="firstInnerDelegate" id="BPMNShape_firstInnerDelegate">
        <omgdc:Bounds height="55.0" width="105.0" x="800.0" y="319.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="1090.0" y="361.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="startevent4" id="BPMNShape_startevent4">
        <omgdc:Bounds height="36.0" width="35.0" x="295.0" y="361.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="firstOuterDelegate" id="BPMNShape_firstOuterDelegate">
        <omgdc:Bounds height="55.0" width="105.0" x="370.0" y="351.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="secondOuterDelegate" id="BPMNShape_secondOuterDelegate">
        <omgdc:Bounds height="55.0" width="105.0" x="520.0" y="351.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="thirdOuterDelegate" id="BPMNShape_thirdOuterDelegate">
        <omgdc:Bounds height="55.0" width="105.0" x="966.0" y="352.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="1170.0" y="358.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow21" id="BPMNEdge_flow21">
        <omgdi:waypoint x="785.0" y="407.0"></omgdi:waypoint>
        <omgdi:waypoint x="870.0" y="407.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow22" id="BPMNEdge_flow22">
        <omgdi:waypoint x="800.0" y="346.0"></omgdi:waypoint>
        <omgdi:waypoint x="732.0" y="380.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow23" id="BPMNEdge_flow23">
        <omgdi:waypoint x="705.0" y="346.0"></omgdi:waypoint>
        <omgdi:waypoint x="800.0" y="346.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow25" id="BPMNEdge_flow25">
        <omgdi:waypoint x="330.0" y="379.0"></omgdi:waypoint>
        <omgdi:waypoint x="370.0" y="378.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow27" id="BPMNEdge_flow27">
        <omgdi:waypoint x="475.0" y="378.0"></omgdi:waypoint>
        <omgdi:waypoint x="520.0" y="378.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow28" id="BPMNEdge_flow28">
        <omgdi:waypoint x="931.0" y="379.0"></omgdi:waypoint>
        <omgdi:waypoint x="966.0" y="379.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow29" id="BPMNEdge_flow29">
        <omgdi:waypoint x="1071.0" y="379.0"></omgdi:waypoint>
        <omgdi:waypoint x="1090.0" y="378.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow30" id="BPMNEdge_flow30">
        <omgdi:waypoint x="625.0" y="378.0"></omgdi:waypoint>
        <omgdi:waypoint x="660.0" y="379.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="1131.0" y="375.0"></omgdi:waypoint>
        <omgdi:waypoint x="1170.0" y="375.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="212.0" y="375.0"></omgdi:waypoint>
        <omgdi:waypoint x="275.0" y="375.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
  • flowable.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="processEngineConfiguration"
		class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">

		<!-- <property name="jdbcUrl"
			value="jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000;MVCC=TRUE" />
		<property name="jdbcDriver" value="org.h2.Driver" />
		<property name="jdbcUsername" value="sa" />
		<property name="jdbcPassword" value="" /> -->
		<property name="asyncFailedJobWaitTime" value="1" />

		<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/postgres" />
		<property name="jdbcDriver" value="org.postgresql.Driver" />
		<property name="jdbcUsername" value="postgres" />
		<property name="jdbcPassword" value="" />

		<property name="databaseSchemaUpdate" value="true" />
		<property name="asyncExecutorActivate" value="true" />

		<property name="asyncExecutor">
			<bean id="asyncExecutor"
				class="org.flowable.job.service.impl.asyncexecutor.DefaultAsyncJobExecutor">
				<property name="retryWaitTimeInMillis" value="150000" />

				<property name="corePoolSize" value="2" />
				<property name="maxPoolSize" value="10" />
				<!-- <property name="keepAliveTime" value="5000" /> -->
				<property name="queueSize" value="100" />
				<property name="maxTimerJobsPerAcquisition" value="1" />
				<property name="maxAsyncJobsDuePerAcquisition" value="1" />
				<property name="defaultAsyncJobAcquireWaitTimeInMillis"
					value="1000" />
				<property name="defaultTimerJobAcquireWaitTimeInMillis"
					value="1000" />
				<property name="timerLockTimeInMillis" value="3000" />
				<property name="asyncJobLockTimeInMillis" value="3000" />
			</bean>
		</property>

		<property name="history" value="none" />
		<property name="asyncHistoryEnabled" value="false" />

	</bean>
</beans>
  • Delegates
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class FirstOuterDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("First outer delegate executed!");
	}

}

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class SecondOuterDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution arg0) {
		System.out.println("Second outer delegate executed!");
	}

}

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class ThirdOuterDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution arg0) {
		System.out.println("Third outer delegate executed!");
	}

}

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class FirstInnerDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution arg0) {
		System.out.println("First inner delegate executed!");
	}

}

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;

public class SecondInnerDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("Second Inner delegate executed! Variable: " + execution.getVariable("testVariable"));
	}

}
  • Test
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.flowable.engine.test.Deployment;
import org.flowable.engine.test.FlowableRule;
import org.junit.Rule;
import org.junit.Test;

public class TestProcessTest {

	@Rule
	public FlowableRule rule = new FlowableRule();

	@Test
	@Deployment(resources = "test.bpmn")
	public void testExecutingServiceTaskInMultiinstanceAsyncExclusiveSubprocess() {
		Map<String, Object> variables = new HashMap<>();
		variables.put("testCollection", Arrays.asList("1", "2"));
		variables.put("outerVariable", 1);
		rule.getRuntimeService().startProcessInstanceByKey("testProcess", variables);
	}

}

Thanks in advance,
Iliyan

1 Like