Optimistic lock exception when trying to do any action to process instance

Hello , I am new to camunda I i integrate the process engine to create a workflow process in my spring boot application. I need to deal some actions to my workflow like adding variable to process, assign a task to a member but every time i receive OptimisticLocking Exception . Can any one help me to manage the transactions with process api . What is the advantage to working with StandaloneProcessEngine or SpringProcessEngine

Hi Amira,

Could you please add the full stack trace of the exception and your code which causes it.

Best regards,
Yana

hello,
Here is the application.yml file with the configuration of the database and the spring process engine


server:
    port: 8081
spring:
   jpa:
        open-in-view: false
        hibernate: 
            ddl-auto: create-drop
            
   datasource:
       url: jdbc:oracle:thin:@localhost:1521:xe
       name: AlfrescoCamunda
       username: AlfrescoCamunda
       password: AlfrescoCamunda
       driverClassName: oracle.jdbc.driver.OracleDriver
       hikari:
                maximum-pool-size:  5
                connection-timeout: 6000      
camunda:
  bpm:
    database:
      table-prefix: "ALFRESCOCAMUNDA."
      schema-update: "true"
    auto-deployment-enabled: true
    job-execution:
      enabled: true
      max-pool-size: 10
      core-pool-size: 5

for the time i test with this simple bpmn example

<?xml version="1.0" encoding="UTF-8" ?>
<definitions xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:ns0="xmlns" ns0:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:ns1="xmlns" ns1:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:ns2="xmlns" ns2:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:ns3="xmlns" ns3:di="http://www.omg.org/spec/BPMN/20100524/DI" id="definitions_a6e48fae-567c-4472-b6dd-24f8284f27d4" targetNamespace="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
  <process id="process1" name="process1" isExecutable="true" camunda:historyTimeToLive="5">
    <startEvent camunda:formKey="process1" id="startEvent"   name="startEvent">
      <outgoing>startEvent-reception</outgoing>
    </startEvent>
    <userTask camunda:candidateGroups="group1, group2, group3"  camunda:candidateUsers="user1, user2, user3" camunda:formKey="" id="reception" name="reception">
      <extensionElements>
        <camunda:formData>
          <camunda:formField id="decision" label="decision" type="enum">
            <camunda:validation>
              <camunda:constraint name="required"/>
              <camunda:constraint config="1000" name="max"/>
            </camunda:validation>
            <camunda:value id="Prolongation" name="Prolongation"/>              
            <camunda:value id="Regularisation" name="Regularisation"/>
            <camunda:value id="Validation" name="Validation"/>
          </camunda:formField>
        </camunda:formData>
      </extensionElements>
      <incoming>startEvent-reception</incoming>
      <incoming>gatewayExclusive1-reception</incoming>
    </userTask>
    <exclusiveGateway id="gatewayExclusive1" name="gatewayExclusive1">
      <outgoing>gatewayExclusive1-reception</outgoing>
      <outgoing>gatewayExclusive1-endEvent</outgoing>
    </exclusiveGateway>
    <endEvent id="endEvent" name="endEvent">
      <incoming>gatewayExclusive1-endEvent</incoming>
    </endEvent>
    <sequenceFlow id="startEvent-reception" sourceRef="startEvent" targetRef="reception"/>
    <sequenceFlow id="gatewayExclusive1-reception" sourceRef="gatewayExclusive1" targetRef="reception">
      <conditionExpression id="conditionExpression_3ec4f78e-3e8e-49bc-a9ec-19bd6197d8d8">${decision.equals('Prolongation')}</conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="gatewayExclusive1-endEvent" sourceRef="gatewayExclusive1" targetRef="endEvent">
      <conditionExpression id="conditionExpression_34cee03e-d2ec-4e35-a139-68dc84e6c270">${decision.equals('Regularisation')}</conditionExpression>
    </sequenceFlow>
  </process>
</definitions>

I start a new instance using this service that’s go well


@Transactional
	public ProcessInstance startProcessInstance(String processDefinition, Map<String, Object> variables) {

		try {
			if (variables == null) {
				
				return runtimeService.startProcessInstanceByKey(processDefinition);
			     
			} else {
				return runtimeService.startProcessInstanceByKey(processDefinition, variables);
			}
		} catch (ProcessEngineException e) {
			throw new ActivitiServiceException(e.getMessage(), e.getCause());
		}
	}

but when i try to assign the current task “reception” using this service i receive the Optimistic Lock exception


  @Transactional
	public void completeTask(String taskKey, Map<String, Object> variables) {
		try {
		    taskService.complete(taskKey, variables);
			//runtimeService.setVariables(taskKey, variables);

		} catch (ProcessEngineException e) {
			e.printStackTrace();
			throw new ActivitiServiceException(e.getMessage(), e.getCause());
		}

	} 

org.camunda.bpm.engine.OptimisticLockingException: ENGINE-03005 Execution of 'UPDATE TaskEntity[4d7b6c6b-4c7c-11e8-b901-5a00e3f22521]' failed. Entity was updated by another transaction concurrently.
	at org.camunda.bpm.engine.impl.db.EnginePersistenceLogger.concurrentUpdateDbEntityException(EnginePersistenceLogger.java:132)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.handleOptimisticLockingException(DbEntityManager.java:484)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.checkFlushResults(DbEntityManager.java:443)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flushDbOperations(DbEntityManager.java:360)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flushDbOperationManager(DbEntityManager.java:318)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flush(DbEntityManager.java:290)
	at org.camunda.bpm.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:203)
	at org.camunda.bpm.engine.impl.interceptor.CommandContext.close(CommandContext.java:132)
	at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:113)
	at org.camunda.bpm.engine.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:42)
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
	at org.camunda.bpm.engine.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:40)
	at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:66)
	at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30)
	at org.camunda.bpm.engine.impl.TaskServiceImpl.claim(TaskServiceImpl.java:165)
	at loanApprovals.CamundaService.claimTaskToUser(CamundaService.java:424)
	at loanApprovals.CamundaService$$FastClassBySpringCGLIB$$fbb20a1e.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
	at loanApprovals.CamundaService$$EnhancerBySpringCGLIB$$a07fb095.claimTaskToUser(<generated>)
	at loanApprovals.CamundaController.claimTasks(CamundaController.java:171)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

best regards

Hi @amira

I adjusted the format of your post so it is more readable. (you can check it for future usage)
As per assign of the task:

this is the completion, the assignation should look like:
taskService.setAssignee(...)

Best regards,
Yana