org.camunda.bpm.engine.OptimisticLockingException: ENGINE-03005 Execution of 'DELETE MessageEntity[9aba61f6-7889-11ea-9059-acde48001122]' failed

Hello everyone,
I am little newbie to Camunda and exploring version 7.11 and running with spring boot 2.1.1.RELEASE.

My process model is quite straight forward and has couple of user tasks, 1 service task (invoking HTTP endpoint) and one task at the end that only waits for 20 seconds (using thread.sleep) before completing the workflow.

I am noticing intermittent error as listed below. I am using all default config for job executor. This error comes up at my last task, which does nothing but Thread.sleep(20000);. Before this, I have multiple user tasks, which are marked completed using REST invocations and no errors initially.

org.camunda.bpm.engine.OptimisticLockingException: ENGINE-03005 Execution of 'DELETE MessageEntity[9aba61f6-7889-11ea-9059-acde48001122]' failed. Entity was updated by another transaction concurrently.
	at org.camunda.bpm.engine.impl.db.EnginePersistenceLogger.concurrentUpdateDbEntityException(EnginePersistenceLogger.java:135)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.handleOptimisticLockingException(DbEntityManager.java:499)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.checkFlushResults(DbEntityManager.java:451)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flushDbOperations(DbEntityManager.java:367)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flushDbOperationManager(DbEntityManager.java:325)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flush(DbEntityManager.java:297)
	at org.camunda.bpm.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:208)
	at org.camunda.bpm.engine.impl.interceptor.CommandContext.close(CommandContext.java:137)
	at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:116)
	at org.camunda.bpm.engine.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:46)
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
	at org.camunda.bpm.engine.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:44)
	at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:70)
	at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33)
	at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobHelper.executeJob(ExecuteJobHelper.java:51)
	at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobHelper.executeJob(ExecuteJobHelper.java:44)
	at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobsRunnable.executeJob(ExecuteJobsRunnable.java:92)
	at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobsRunnable.run(ExecuteJobsRunnable.java:61)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)

Also, I have created a custom bpmn parse listener to apply all to of my workflows.

@Component
public class AsyncExecutionEventListener extends AbstractBpmnParseListener {
    @Override
    public void parseStartEvent(Element startEventElement, ScopeImpl scope,
                                ActivityImpl startEventActivity) {
        startEventActivity.setAsyncAfter(true);
    }

    @Override
    public void parseServiceTask(Element serviceTaskElement, ScopeImpl scope, ActivityImpl serviceTaskActivity) {
        serviceTaskActivity.setAsyncAfter(true);
    }

    @Override
    public void parseUserTask(Element userTaskElement, ScopeImpl scope, ActivityImpl userTaskActivity) {
        userTaskActivity.setAsyncAfter(true);
    }
}

Can anyone point me in right direction?
Appreciate!

to fully understand whats going on i suggest reading the best practice guide on how threads work within the engine.
https://camunda.com/best-practices/performance-tuning-camunda/#_understanding_thread_handling

Thank you Niall for sharing this link. I’ve gone through this link and I still don’t get reasoning behind this intermittent failures.

Few points here.

  • I am using exclusive (default) jobs, which is meant not to raise this optimistic locking exception
  • I dont have any parallel paths either
  • I don’t have external tasks, but I do have user tasks, which are marked completed using camunda API successfully. Issue comes up when I am running a simple task, which does nothing but just simulates long running (as much 20-50 seconds) using thread.sleep and that causes this failure.