Error migration a process

As we try to migrate the process we keep getting this error:

2020-09-21 15:05:22,320 [default task-1] ERROR org.flowable.common.engine.impl.interceptor.CommandContext Error while closing command context    org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: The DELETE statement conflicted with the REFERENCE constraint "ACT_FK_TIMER_JOB_PROCESS_INSTANCE". The conflict occurred in database "WF", table "i2sflow.ACT_RU_TIMER_JOB", column 'PROCESS_INSTANCE_ID_'.
### The error may exist in org/flowable/db/mapping/entity/Execution.xml
### The error may involve org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl.deleteExecution-Inline
### The error occurred while setting parameters
### SQL: delete from ACT_RU_EXECUTION where ID_ = ? and REV_ = ?
### Cause: The DELETE statement conflicted with the REFERENCE constraint "ACT_FK_TIMER_JOB_PROCESS_INSTANCE". The conflict occurred in database "WF", table "i2sflow.ACT_RU_TIMER_JOB", column 'PROCESS_INSTANCE_ID_'.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(
at org.apache.ibatis.session.defaults.DefaultSqlSession.delete(
at org.flowable.common.engine.impl.db.DbSqlSession.flushDeleteEntities(
at org.flowable.common.engine.impl.db.DbSqlSession.flushDeletes(
at org.flowable.common.engine.impl.db.DbSqlSession.flush(
at org.flowable.common.engine.impl.interceptor.CommandContext.flushSessions(
at org.flowable.common.engine.impl.interceptor.CommandContext.close(
at org.flowable.common.engine.impl.interceptor.CommandContextInterceptor.execute(
at org.flowable.common.spring.SpringTransactionInterceptor.lambda$execute$0(
at org.flowable.common.spring.SpringTransactionInterceptor.execute(
at org.flowable.common.engine.impl.interceptor.LogInterceptor.execute(
at org.flowable.common.engine.impl.cfg.CommandExecutorImpl.execute(
at org.flowable.common.engine.impl.cfg.CommandExecutorImpl.execute(
at org.flowable.engine.impl.RuntimeServiceImpl.migrateProcessInstancesOfProcessDefinition(
at org.flowable.engine.impl.migration.ProcessInstanceMigrationBuilderImpl.migrateProcessInstances(
at pt.i2s.fwk.workflow.processes.beans.ProcessDefinitionMigrationService.migrateProcessInstances(
... 73 more
Caused by: The DELETE statement conflicted with the REFERENCE constraint "ACT_FK_TIMER_JOB_PROCESS_INSTANCE". The conflict occurred in database "WF", table "i2sflow.ACT_RU_TIMER_JOB", column 'PROCESS_INSTANCE_ID_'.
at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.execute(
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(
at org.apache.ibatis.executor.BaseExecutor.update(
at org.apache.ibatis.executor.CachingExecutor.update(
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(
... 88 more

At first i thought that i was doing something wrong, so i double checked everything, the processes were fine but with more investigation we found out that the query that the engine was trying to do was delete from ACT_RU_EXECUTION where ID_ = ‘58b804eb-15b5-11ea-b7a8-00155dfc8176’ and REV_ = 2 and this query is what is causing the error.
For that process instance in question we have 2 records on the table ACT_RU_EXECUTION and the process migration fails due to a FK constraint as we can see here:

Some more information about this process, it has a timer job and after the timer a sub process is called with a single service task in it.
Also, we are using Flowable 6.4.2.
How can i solve this problem?

Thanks in advance.

Hard to say without the process model. Can you share something so we can try to reproduce the problem?

Hi @joram

I’ve found a way to isolate the problem.
We have an event listener on the process that listens for “JOB_CANCELED” events. This listener deletes the process instance once it’s triggered.

I’m not sure what’s happening behind the scenes when Flowable is performing the migration, but for some reason, this listener is being triggered and I think that’s why you get this error. I’ve tried migrating the exact same process without the listener and the error does not occur.
It looks to me like this listener shouldn’t be triggered at that time.

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="" xmlns:xsi="" xmlns:xsd="" xmlns:flowable="" xmlns:bpmndi="" xmlns:omgdc="" xmlns:omgdi="" typeLanguage="" expressionLanguage="" targetNamespace="ModuleMacroProcess">
  <process id="bpmnMigration" name="Bpmn Migration" isExecutable="true" flowable:candidateStarterGroups="flowableUser">
      <flowable:eventListener events="JOB_CANCELED" class="com.flowable.bpmn.migration.CancelProcess" onTransaction="committed"></flowable:eventListener>
    <startEvent id="startnoneevent1" flowable:initiator="initiator" flowable:formFieldValidation="false"></startEvent>
    <intermediateCatchEvent id="theTimer" name="Timer">
    <sequenceFlow id="sequenceFlow2" sourceRef="startnoneevent1" targetRef="theTimer">
    <endEvent id="endNoneEvent1">
    <sequenceFlow id="sequenceFlow3" sourceRef="theTimer" targetRef="scriptTask1">
    <scriptTask id="scriptTask1" name="Script task" scriptFormat="groovy" flowable:autoStoreVariables="false">
      <script><![CDATA[sum = 0
      test = 2
    for ( i in inputArray ) {
      sum += i
    <sequenceFlow id="sequenceFlow4" sourceRef="scriptTask1" targetRef="endNoneEvent1">
package com.flowable.bpmn.migration;

import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.ProcessEngines;
import org.flowable.job.service.impl.persistence.entity.TimerJobEntity;

public class CancelProcess implements FlowableEventListener {
    public void onEvent(FlowableEvent event) {

        if (event.getType().equals(FlowableEngineEventType.JOB_CANCELED)) {
            FlowableEntityEvent flowableEntityEvent = (FlowableEntityEvent) event;
            TimerJobEntity timerJobEntity = (TimerJobEntity) flowableEntityEvent.getEntity();
            ProcessEngines.getDefaultProcessEngine().getRuntimeService().deleteProcessInstance(timerJobEntity.getProcessInstanceId(), "TEST");

    public boolean isFailOnException() {
        return true;

    public boolean isFireOnTransactionLifecycleEvent() {
        return false;

    public String getOnTransaction() {
        return null;

Is there anything that can be done to prevent this from happening?
I have considered disabling the eventDispatcher while this migration is happening but I’m afraid that might have negative side effects…

Thanks @DavidPLamas, that’s very useful. Will try to reproduce it with that.

That’s an option indeed. If nothing else is running at that moment, the event dispatched can be disabled. However, I assume other instances will still need it while this one is being migrated?

Yes, you’re right.
I was thinking about creating a jar/war application just to migrate this process. It would set up the engine using the same configuration but disabling the event dispatcher.
Do you think this is viable?