Variables in a subProcess are being overwritten when Multi Instance is enabled

Hello,
I’m trying to use Parallel Multi-Instance in a subProcess. Looks like that camunda is not creating a separate instance for each parallel process being called. And due to this, the Variables of the subprocess are being overwritten. Is there something that I can do to overcome this.

Below is the configuration I have used:

Multi-Instance:-

Loop Cardinality:
Collection: flattenRuleList
Element Variable: L1FlowMatchingRules
Completion Condition:

Multi-Instance Async Before: Check

Async Continuations:
Async Before: Check
Async After:
Exclusive: Check.

I have same problem in my process. I concatenated the loopCounter value to variable name.

Thanks for your response. Can you please share a sample bpmn on how to add the loopCounter.

Here is the java example:
Integer loopCounter = (Integer) execution.getVariable(“loopCounter”);
String currentVariableName = String.format(“myVariableName-%d”, loopCounter);

1 Like

Take a look at this thread Pattern Review: DMN Looping for Array Input. This seems to be the issue you are describing, and there are strategies in the thread on how to setup your multi instance to avoid the overwrite

Thanks for the Suggestion @StephenOTT.
I have looked at your project. I’ll update you if your solution works for me.

Hello @StephenOTT,
I tried using your solution but I had no luck.
Current Issue :
The subprocess in the BPMN runs as a parallel instance. There are n number of variables inside the subprocess. What I have observed is that these variables are overwritten while the process runs in the parallel instance.

Is there a solution for this issue like defining a scope for variables while running in parallel instance.

What version of camunda are you using?

When you run in parallel you cannot be updating the same variable without the chance of "lock exceptions occurring. You are trying to write to the same DB table row (the variable) multiple parallel instances at the same time.

Here is a solution to deal with concurrent writes to a variable: https://github.com/StephenOTT/camunda-concurrency-helpers-process-engine-plugin

Another approach we use is to init the variables in each loop scope before they get assigned a value. This can be done with a script listener on the very first node of the MI and just calling execution.setLocalVariable. The issue we ran into is that the resultVariable of a ServiceTask is not saved to a local variable by default and is assigned to a variable at the parent scope, being the multi instance. Therefore, subsequent instances just update the same variable as they run, overwriting the previous value. If you explicitly create the local variables for each instance, those will be updated because they are considered first if they exist.

@tiesebarrell are you aggregating any values into the parent scope?

Not with that particular instance, no. In that case we are simply performing a for-each action.


Take for example the BPMN , I am facing an issue in the inner most sub process where I get duplicate values . I want to uniquely access every value with help of loop counter. How do I set the collection field in BPMN for it to be accessed uniquely? Currently I have set the collection name eg. the array list of values .

If you’ve nested the multi instances and use collections, you’ll have to make sure to have a unique collection for each instance, that serves as the collection for the nested multi-instance. The strategies described above work similarly for the nested situation, they just also apply to the variables for the collections.

So, you could create collections which are suffixed with the loop counter as the OP suggested, or you can use Stephen’s approach, or you could pre-initialise local execution variables for each iteration as I suggested.

1 Like