How can i avoid OptimisticLockingException using runtimeService in scheduler?

Hi!

I have scheduler, which do next steps:

  1. Scheduler get list of all unfinished processes, which older than 60 days (executes every night).
  2. Runtime service delete all process instances from this list

Code in scheduler looks like this:

historyService.createHistoricProcessInstanceQuery()
        .startedBefore(startedBefore)
        .unfinished()
        .list()
        .forEach(processInstance -> {
            runtimeService.deleteProcessInstance(processInstance.getId(), "Terminate long running process");
        });

Deleting a process instance causes an OptimisticLockingException error (because of runtimeService.deleteProcessInstance(...)).

Log:

OptimisticLockingException: ENGINE-03005 Execution of 'DELETE ExternalTaskEntity[2dcd983e-c961-11e9-aef4-0a58c0a83f07]' failed. Entity was updated by another transaction concurrently

How can i avoid concurrency problem?

Hi,

You have configured History cleanup [[1]]
The execution of the History cleanup (execution of EverLivingJob) shouldn’t be affected and shouldn’t create OptimisticLockingException situation during the process deletion in my opinion.
Are you sure that the exception is caused by the provided code?
What is your setup (Camunda version, server, database)? Could you please attach the complete stack trace of the exception.

[[1]]: https://docs.camunda.org/manual/7.11/user-guide/process-engine/history/#history-cleanup

Best regards,
Yana

@Yana Hello! I’am glad to meet you here, Yana :slightly_smiling_face:

Are you sure that the exception is caused by the provided code?

Yes, because logs tells me about deleting process instance:

org.camunda.bpm.engine.impl.cmd.AbstractDeleteProcessInstanceCmd.deleteProcessInstance(AbstractDeleteProcessInstanceCmd.java:75)

We can also see concurrency issues here:

OptimisticLockingException: ENGINE-03005 Execution of 'DELETE ExternalTaskEntity[2dcd983e-c961-11e9-aef4-0a58c0a83f07]' failed. Entity was updated by another transaction concurrently.\n\tat org.camunda.bpm.engine.impl.db.EnginePersistenceLogger.concurrentUpdateDbEntityException(EnginePersistenceLogger.java:134)
...
RuntimeServiceImpl.java:173)\n\tat com.tm.wg.scheduler.TmProcessTerminationScheduler.lambda$terminateProcess$0(TmProcessTerminationScheduler.java:33)\n\tat java.util.ArrayList.forEach(ArrayList.java:1257)\n\tat com.tsystems.tm.wg.scheduler.TmProcessTerminationScheduler.terminateProcess(TmProcessTerminationScheduler.java:31)

What is your setup (Camunda version, server, database)?

Camunda engine version: 7.10, environment: OpenShift, database: PostgreSQL.

File with full stack trace (there are two logs here, because in our environment there are two pods of camunda microservice). They are separated by a space.
logs.json (13.2 KB)

I think you need to understand better how the optimistic locking mechanism is working.
My assumption is that the process was execution this External Task when the deletion was invoked. It is normal if two transactions are running in parallel and they update the same entity that an optimistic locking exception to be thrown. You can read more about here: https://docs.camunda.org/manual/latest/user-guide/process-engine/transactions-in-processes/#the-optimisticlockingexception