NullValueException: execution doesn't exist: execution is null

Camunda Spring Boot, 2.3.0
Camunda 7.8.0
Spring Boot 2.0.1

I’m listening Camunda process events, for example HistoricProcessInstanceEventEntity event.
Inside this event there is execution ID: eb552111-51c2-11e8-b8b2-0242ac120014

When i call
runtimeService.getVariable(historyEvent.getExecutionId(), “starter”)
then error is throwd:
org.camunda.bpm.engine.exception.NullValueException: execution eb552111-51c2-11e8-b8b2-0242ac120014 doesn’t exist: execution is null

When i go the database, i can see the expected row there:
select * from act_ru_execution where id_=‘eb552111-51c2-11e8-b8b2-0242ac120014’

Why i get this error? Can this possible that process data is not yet commited in database level?



Because i have 2 handlers: DbHistoryEventHandler and custom handler, the data is not saved to database yet by DbHistoryEventHandler what custom handler is looking for.

Dirty workaround is simple timeout to my custom handler:
try {
_ TimeUnit.SECONDS.sleep(1);_
_ } catch (InterruptedException e) {_
_ // TODO Auto-generated catch block_
_ e.printStackTrace();_
_ }_

Yes, that’s what’s happening here, the default historic events are synchronous and in transaction, so when you use a service to query state, you cannot see it yet.

Can we add our custom handler to queue?
I mean, custom handler will not be executed before DbHistoryHandler is completed?

You can define a custom history listener, but this also gets called in sync. If you need async behavior, you could create a job from your custom listener or switch to async execution (spring @Async for example)

In addition to Jan’s comment, here’s how you can do things after the current transaction is committed: Why can’t I see the end event during a process instance end event listener?

However, keep in mind that in case when your transaction listener fails that the process engine transaction won’t be rolled back. So you may need proper error handling to avoid losing history events.