REST integration issues with setVariable in between REST calls

Hi,

In an asynchronous task I’m doing 3 REST calls to another system in sequence. (It tries to create 3 entities in the other system)

When the second or last call fails, I do not want to do the other calls again, so I tried storing the newly fetched variables in the engine afterwards by overwriting the existing marshalled JSON variable.

Example;

selectedUser = client.saveUser(selectedCompany.getId(), selectedUser);
execution.setVariable(VariableConstants.SELECTED_USER, selectedUser);

Unfortunately these setVariable actions are part of the transaction.
I tried following solution which creates a new tx to set the variable in but that resulted in a db exception. (Something that I cannot delete the previous bytearray value without deleting the variable linked to it.)

User finalSelectedUser = selectedUser;
processEngineConfiguration.getCommandExecutorTxRequiresNew().execute((Command<Void>) commandContext -> {
        execution.setVariable(VariableConstants.SELECTED_USER, finalSelectedUser);
        return null;
});

17:26:00,620 ERROR [org.camunda.bpm.engine.context] (pool-7-thread-7) ENGINE-16004 Exception while closing command context: ENGINE-03004 Exception while executing Database Operation ‘DELETE_BULK deleteByteArrayNoRevisionCheck 86be3d78-41fb-11e6-b231-ac87a3204ca5’ with message ’

Error updating database. Cause: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: “ACT_FK_VAR_BYTEARRAY: PUBLIC.ACT_RU_VARIABLE FOREIGN KEY(BYTEARRAY_ID_) REFERENCES PUBLIC.ACT_GE_BYTEARRAY(ID_) (‘86be3d78-41fb-11e6-b231-ac87a3204ca5’)”; SQL statement:

delete from ACT_GE_BYTEARRAY where ID_ = ? [23503-168]

The error may involve org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntity.deleteByteArrayNoRevisionCheck-Inline

The error occurred while setting parameters

SQL: delete from ACT_GE_BYTEARRAY where ID_ = ?

Cause: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: “ACT_FK_VAR_BYTEARRAY: PUBLIC.ACT_RU_VARIABLE FOREIGN KEY(BYTEARRAY_ID_) REFERENCES PUBLIC.ACT_GE_BYTEARRAY(ID_) (‘86be3d78-41fb-11e6-b231-ac87a3204ca5’)”; SQL statement:

delete from ACT_GE_BYTEARRAY where ID_ = ? [23503-168]'. Flush summary:
[
INSERT HistoricVariableUpdateEventEntity[9ce32fbb-41fb-11e6-b231-ac87a3204ca5]
INSERT ByteArrayEntity[9ce32fb9-41fb-11e6-b231-ac87a3204ca5]
INSERT ByteArrayEntity[9ce32fba-41fb-11e6-b231-ac87a3204ca5]
INSERT ByteArrayEntity[9ce356cc-41fb-11e6-b231-ac87a3204ca5]
DELETE_BULK deleteByteArrayNoRevisionCheck 86be3d78-41fb-11e6-b231-ac87a3204ca5
DELETE_BULK deleteByteArrayNoRevisionCheck 86be3d7b-41fb-11e6-b231-ac87a3204ca5
UPDATE HistoricVariableInstanceEntity[86b53d71-41fb-11e6-b231-ac87a3204ca5]
]: org.camunda.bpm.engine.ProcessEngineException: ENGINE-03004 Exception while executing Database Operation ‘DELETE_BULK deleteByteArrayNoRevisionCheck 86be3d78-41fb-11e6-b231-ac87a3204ca5’ with message ’

Error updating database. Cause: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: “ACT_FK_VAR_BYTEARRAY: PUBLIC.ACT_RU_VARIABLE FOREIGN KEY(BYTEARRAY_ID_) REFERENCES PUBLIC.ACT_GE_BYTEARRAY(ID_) (‘86be3d78-41fb-11e6-b231-ac87a3204ca5’)”; SQL statement:

delete from ACT_GE_BYTEARRAY where ID_ = ? [23503-168]

The error may involve org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntity.deleteByteArrayNoRevisionCheck-Inline

The error occurred while setting parameters

SQL: delete from ACT_GE_BYTEARRAY where ID_ = ?

Cause: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: “ACT_FK_VAR_BYTEARRAY: PUBLIC.ACT_RU_VARIABLE FOREIGN KEY(BYTEARRAY_ID_) REFERENCES PUBLIC.ACT_GE_BYTEARRAY(ID_) (‘86be3d78-41fb-11e6-b231-ac87a3204ca5’)”; SQL statement:

delete from ACT_GE_BYTEARRAY where ID_ = ? [23503-168]'. Flush summary:
[
INSERT HistoricVariableUpdateEventEntity[9ce32fbb-41fb-11e6-b231-ac87a3204ca5]
INSERT ByteArrayEntity[9ce32fb9-41fb-11e6-b231-ac87a3204ca5]
INSERT ByteArrayEntity[9ce32fba-41fb-11e6-b231-ac87a3204ca5]
INSERT ByteArrayEntity[9ce356cc-41fb-11e6-b231-ac87a3204ca5]
DELETE_BULK deleteByteArrayNoRevisionCheck 86be3d78-41fb-11e6-b231-ac87a3204ca5
DELETE_BULK deleteByteArrayNoRevisionCheck 86be3d7b-41fb-11e6-b231-ac87a3204ca5
UPDATE HistoricVariableInstanceEntity[86b53d71-41fb-11e6-b231-ac87a3204ca5]
]

Second thing I tried was to;

  1. Delete existing variable
  2. Create new variable with same name as previous

But this gives me another db exception;

08:50:17,958 ERROR [org.camunda.bpm.engine.context] (pool-7-thread-3) ENGINE-16004 Exception while closing command context: ENGINE-03004 Exception while executing Database Operation 'INSERT VariableInstanceEntity[bc08f3b2-427c-11e6-ac80-ac87a3204ca5]' with message '
### Error updating database.  Cause: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "ACT_UNIQ_VARIABLE_INDEX_C ON PUBLIC.ACT_RU_VARIABLE(VAR_SCOPE_, NAME_)"; SQL statement:
insert into ACT_RU_VARIABLE
    (
      ID_,
      TYPE_,
      NAME_,
      PROC_INST_ID_,
      EXECUTION_ID_,
      CASE_INST_ID_,
      CASE_EXECUTION_ID_,
      TASK_ID_,
      BYTEARRAY_ID_,
      DOUBLE_,
      LONG_,
      TEXT_,
      TEXT2_,
      VAR_SCOPE_,
      SEQUENCE_COUNTER_,
      IS_CONCURRENT_LOCAL_,
      TENANT_ID_,
      REV_
    )
    values (
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      ?,
      1
    ) [23505-168]
### The error may involve org.camunda.bpm.engine.impl.persistence.entity.VariableInstanceEntity.insertVariableInstance-Inline
### The error occurred while setting parameters
### SQL: insert into ACT_RU_VARIABLE     (       ID_,       TYPE_,       NAME_,       PROC_INST_ID_,       EXECUTION_ID_,       CASE_INST_ID_,       CASE_EXECUTION_ID_,       TASK_ID_,       BYTEARRAY_ID_,       DOUBLE_,       LONG_,       TEXT_,       TEXT2_,       VAR_SCOPE_,       SEQUENCE_COUNTER_,       IS_CONCURRENT_LOCAL_,       TENANT_ID_,       REV_     )     values (       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       ?,       1     )

Anyone who has some kind of solution to skip the rest calls that have been successful?

Hi @nvanbelle,

the easiest way is to use three service tasks - one for each rest call. So you can control the transaction boundaries and handle the failure of one call.

Best regards,
Philipp

Hi Philipp,

Yes, it crossed my mind a few times, but in my opinion the bpmn mobel becomes too technical in such a case.

But I guess when there is absolutely no way of saving a variable in the engine and keeping it there wether or not the task fails in the end, I will have to do it like that.

Thank you for the response.
Kind regards