Question about transactions and asynchronous

Hello guys!! I am facing some problems here about asynchronous task and transactions. Let’s suppose i have this model:

In “Task 1” i make an request to an external API. If that request fails i update a camunda variable and use this variable in the gateway to check if there were more than 2 retries.

All my tasks: “Task 1” and “Task 2” have the option “Asynchronous before” set, because i want a new transaction open in the beginning of every task.

The problem that i am facing is that: in “Task 1” when an error occurs i do the following: “execution.setVariable(TASK1_RETRIES.getKey(), counter)”. But when the gateway tries to validate this variable it’s value wasn’t updated yet, so it’s reading and old value. So i imagine it’s happening something with transactions.

Sorry if this is a silly question. I’ve already tried to read the docs but haven’t found anything useful to solve this problem.

Thanks!

How do you update the variable? Do you read it from variables first if there is already an existing variable value and then increment and complete the task?

Doesn’t have any sens to me that get’s updated only first time (to number 1) and afterwards doesn’t save to number 2, or maybe I didn’t understand you well.

Of topic,
I would do it in another way in bpmns sens.
You can maintain retries in the service task and raise an error from there, hence you will have an error boundary message to the service task that leads to the Error service task.