Problem when updating process variables

Hi,
In our process, we use a lot of json variables. As they are used in the gateways, they have to be set even if they are not actually filled with data (yet). I now run into problems when I try to update them in the course of the process:

 > Object infoJson = runtimeService.getVariable(processInstanceID, "infos");
>         List<Object> infos =  new ObjectMapper().readValue((String) infoJson, List.class);
>         Map<String, String> infoElement = newHashMap();
>         infoElement.put("bugs", 3);
>         infos.add(infoElement);

>         runtimeService.setVariable(processInstanceID, "infos", new ObjectMapper()
>                 .writerWithDefaultPrettyPrinter().writeValueAsString(infos));

The exception I get is:

org.camunda.bpm.engine.ProcessEngineException: ENGINE-03004 Exception while executing Database Operation 'INSERT IdentityLinkEntity[7d18a44c-7dbc-11e7-8e63-7a3a092e9fec]' with message '
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`bpm_dev_local`.`act_ru_identitylink`, CONSTRAINT `ACT_FK_TSKASS_TASK` FOREIGN KEY (`TASK_ID_`) REFERENCES `ACT_RU_TASK` (`ID_`))
### The error may involve org.camunda.bpm.engine.impl.persistence.entity.IdentityLinkEntity.insertIdentityLink-Inline
### The error occurred while setting parameters
### SQL: insert into ACT_RU_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_DEF_ID_, TENANT_ID_, REV_ )     values (?,             ?,             ?,             ?,             ?,             ?,             ?,             1            )
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`bpm_dev_local`.`act_ru_identitylink`, CONSTRAINT `ACT_FK_TSKASS_TASK` FOREIGN KEY (`TASK_ID_`) REFERENCES `ACT_RU_TASK` (`ID_`))'. Flush summary: 
 [
  INSERT HistoricVariableInstanceEntity[76b6baff-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricVariableInstanceEntity[76b6bb00-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricVariableInstanceEntity[76b6e211-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricVariableInstanceEntity[77a783a5-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricVariableInstanceEntity[77a783a6-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricVariableInstanceEntity[7d165a57-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricTaskInstanceEventEntity[7d18a44b-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricProcessInstanceEventEntity[76b693ed-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricActivityInstanceEventEntity[EndEvent_0x3qy79:7d19197d-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricActivityInstanceEventEntity[StartEvent_Main:76b6e213-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricActivityInstanceEventEntity[Task_14bg7eu:76b6e214-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT HistoricActivityInstanceEventEntity[register_form:7d187d3a-7dbc-11e7-8e63-7a3a092e9fec]
  INSERT IdentityLinkEntity[7d18a44c-7dbc-11e7-8e63-7a3a092e9fec]
]
	at org.camunda.bpm.engine.impl.db.EnginePersistenceLogger.flushDbOperationException(EnginePersistenceLogger.java:115)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flushDbOperationManager(DbEntityManager.java:292)
	at org.camunda.bpm.engine.impl.db.entitymanager.DbEntityManager.flush(DbEntityManager.java:278)
	at org.camunda.bpm.engine.impl.interceptor.CommandContext.flushSessions(CommandContext.java:247)
	at org.camunda.bpm.engine.impl.interceptor.CommandContext.close(CommandContext.java:176)
	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.ProcessInstantiationBuilderImpl.executeWithVariablesInReturn(ProcessInstantiationBuilderImpl.java:156)
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:122)
	at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:118)
	at org.camunda.bpm.engine.impl.RuntimeServiceImpl.startProcessInstanceByKey(RuntimeServiceImpl.java:91)

I am quite new to Camunda, but from the docs it seems that it is pretty normal to update process variables. Indeed, for most of them it works - we even reuse variables during the process for different gateways. Any help would be appreciated!

@auringo have you looked at using SPIN?

https://docs.camunda.org/manual/7.7/reference/spin/json/01-reading-json/

Hi Stephen, yes I have looked at it, but we are not using it at the moment. Are you implying that my problem could be related to JSON parsing? As far as I can see, the json is alright and can be parsed (can be checked in the debugger), but the problem happens after that, when saving to the database.

Hi,

Its not 100% clear to me, however are these process variables persisted as a String rather than Json as a bytearray? If so, could it be that you occasionally exceed the 4k storage limit and thus referential integrity breaks down? Could be a long shot, but something to consider…

regards

Rob

The exception does not seem to be directly related to the code you posted. It shows a constraint violation exception that an identity link is inserted that references a non-existing task (e.g. the candidate user relation results in an identity link). Could you please upload a minimal application or unit test to github that reproduces this? You can use the unit testing template to get started: https://github.com/camunda/camunda-engine-unittest