Data loose in parallel process

Hello!

I have process chain with 3 parallel tasks. They all update one json variable (all my form data is in one json).
But unfortunately the latest task in parallel process is overwriting other previous task json data.

Is this a bug (data merge is not working in Camunda side) or perhaps is there any workaround?

Thanks!
Erki

when you update a variable in the process, you set the value. So that means that if you’re working in parallel tasks and all of them are performed at once, some users might be working on stale data, editing and then overwriting the value. If that’s undesirable, you should either perform some locking or merging of your own or consider a process design that’s not parallel.

1 Like

Hi @Erki_Kriks,
This might help you a bit: https://docs.camunda.org/manual/7.5/user-guide/process-engine/variables . The variables you are using are process variables i.e. defined at process level, you should consider using task variables instead to retain those values.

1 Like

The solution which work for me (maybe it is not the best) is to use listener in parallel process where listener merges the jsons:

public class JsonMergeListener implements CaseExecutionListener {

private static Logger logger = Logger.getLogger(JsonMergeListener.class);

@Override
public void notify(DelegateCaseExecution execution) {
	CaseExecutionEntity caseExecutionEntity  = Context.getCommandContext().getCaseExecutionManager().findCaseExecutionById(execution.getId());
	String processId = caseExecutionEntity.getParent().getSuperExecution().getProcessInstanceId();
	Optional<VariableInstanceEntity> optional = Context.getCommandContext().getVariableInstanceManager()
		.findVariableInstancesByProcessInstanceId(processId).stream().filter(v -> "json".equals(v.getName())).findFirst();
	if (optional.isPresent()) {
		VariableInstanceEntity existingVar = optional.get();
		Object extisingJson =  existingVar.getValue();
		Object mergedJson = JsonUtil.mergeJSONObjects(extisingJson, execution.getVariable("json"));
		execution.setVariable("json", mergedJson);
	}
}

}