Problem
If a variable is set by an async start event it is created in the runtime but not in the history as the activity instance id is unknown until the start event is executed. If this variable is changed before the start event job is executed the variable is created in the history. And when the start event job is executed another historic variable instance insert command is created. Which fails with an unique constraint violation as this historic variable instance is already created.
Example
BPMN Process:
AsyncStartEventTest.testAsyncStartEventWithVariableChange.bpmn20.xml
<?xml version="1.0" encoding="UTF-8"?> <definitions id="definitions" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" targetNamespace="Examples"> <process id="testProcess"> <startEvent id="start" camunda:async="true"/> <sequenceFlow id="flow" sourceRef="start" targetRef="end" /> <endEvent id="end" /> </process> </definitions>
JUnit Test:
@Deployment public void testAsyncStartEventWithVariableChange() { VariableMap variables = Variables.putValue("foo", "bar"); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testProcess", variables); Execution execution = runtimeService.createExecutionQuery().singleResult(); runtimeService.setVariable(execution.getId(), "foo", "bar2"); executeAvailableJobs(); assertProcessEnded(processInstance.getId()); }