I am in the process of upgrading from Activiti 5.16.3 to Activiti 5.19.0.1 and came across what appears to be a bug when setting variables under certain circumstances.
In a local JUnit test, I have a workflow which contains a boundary event 1 second timer which in turn invokes a custom JavaDelegate which sets a workflow variable using RuntimeService.setVariable. This eventually invokes VariableScopeImpl.createVariableInstance, however, this method doesn't yet have the variableInstances map initialized (i.e. it is null), so the variable doesn't get set in the map. Then when the workflow ends and the transaction commits, Activiti attempts to insert the process instance history records and delete the process instance runtime instance records. However, since the variable I set isn't present in the variableInstances map, Activiti winds up not deleting it from the runtime instance and in turn, a constraint violation exception is thrown (see below) since it can't delete the runtime process instance with a leftover variable still present.
A simple workaround I can use for the time being is to call RuntimeService.getVariables before I call setVariable which initializes the variableInstances map and fixes the problem. However, I would appreciate it if you could fix this issue.
Thank you.
Exception that is thrown due to the above problem:
org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "ACT_FK_VAR_EXE: PUBLIC.ACT_RU_VARIABLE FOREIGN KEY(EXECUTION_ID_) REFERENCES PUBLIC.ACT_RU_EXECUTION(ID_) ('4')"; SQL statement:
delete from ACT_RU_EXECUTION where ID_ = ? and REV_ = ? [23503-187]
### The error may involve org.activiti.engine.impl.persistence.entity.ExecutionEntity.deleteExecution-Inline
### The error occurred while setting parameters
### SQL: delete from ACT_RU_EXECUTION where ID_ = ? and REV_ = ?
### Cause: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "ACT_FK_VAR_EXE: PUBLIC.ACT_RU_VARIABLE FOREIGN KEY(EXECUTION_ID_) REFERENCES PUBLIC.ACT_RU_EXECUTION(ID_) ('4')"; SQL statement:
delete from ACT_RU_EXECUTION where ID_ = ? and REV_ = ? [23503-187]