Custom query - How to deserialize all Act_ru_variable values?

Hi folks

What is the best way to handle deserializing all types of variables when performing a custom query?
The docs

https://docs.camunda.org/manual/7.5/examples/tutorials/custom-queries/#custom-mybatis-queries

for custom queries refer to example code that is no longer present and while the new example code is alot more concise than what was there previously its still set up to assume simple string variables.

I’d attempted to recreate what the example code does to fetch variables by case execution, mapping the variable fields onto VariableInstanceEntity and querying this object via

getTypedValue(true);

However this fails as in TypedValueField when requesting the VariableSerializer it fails as there is no command context as you can see from the method below

public static VariableSerializers getSerializers() {
    if (Context.getCommandContext() != null) {
     ....
    } else {
      throw LOG.serializerOutOfContextException();
    }
  }

It would seem more correct to go this way to recreate the variables since I’m not familiar with the db structure for the ACT_RU_VARIABLE table (and it has lots of different value columns) and the deserialization would work well for complex types etc.

The old example code in the doc example would have created the command context I believe, whats the correct way to go about executing custom queries from within a command context as of v7.5?

Could you share the code you have on github?

Thanks,
Thorben

Heya @thorben

I’ve put an example up at

Its a tweaked version of the example code for v7.5.

Running the unit test (I ran against the WildFly distrubtion of camunda, this will be tomcat/spring boot outside of the test though ) gives

javax.ejb.EJBException: org.camunda.bpm.engine.ProcessEngineException: ENGINE-03041 Cannot work with serializers outside of command context.

I need to be able to retrieve the variables attached to the task that are not just strings, like the demo was doing and ensure they are deserialized properly to their correct type, eg a stored integer should come back as an integer, string as a string , boolean as a boolean and so on

Hi @embee1981,

Thanks for sharing the code. I actually like the way this forum displays github links :slight_smile:

Variable deserialization the way it is built is not really meant to be used outside of engine commands. Anyway, my best idea to get this would be something like (untested):

VariableInstanceEntity variableInstance = ..;
VariableSerializers serializers = rule.getProcessEngineConfiguration().getVariableSerializers();
TypedValueSerializer<?> serializer = serializers.getSerializerByName(variableInstance.getSerializerName());
TypedValue typedValue = serializer.readValue(variableInstance, true);

Yet, we are quite deep into internal API here, so using this in productive code, you risk rather high manual migration effort when updating to new Camunda versions.

Cheers,
Thorben

Hi,

i have the same problem/question.

@embee1981 Did you try thorbens idea? Does this work?

What about setting the Context with CommandContext?

I used the Example described here: Custom Queries | docs.camunda.org
It is differnt to the example on githab.

Is setting the Engine Context and a new CommandContext to Context before fetching the variables a good idea? I tried this and then you can easily use variableInstanceEntity.getValue()

Context.setCommandContext(MyBatisExtendedSessionFactory.getInstance().getCommandContextFactory().createCommandContext());
Context.setProcessEngineConfiguration(MyBatisExtendedSessionFactory.getInstance());

But using this, i got a Exception using CDI, described in an older ticket: https://app.camunda.com/jira/browse/CAM-3183
So before fetching the variables, i set the Context, after fetching, i set the context to null. The Context.getCommandContext method looks ThreadSave.
But there is another problem left. It doesn’t work vor serialized Objects. We don’t want to use them, so i didn’t look into it.

What i did feels more like a hack and i think everyone that uses variables in custom queries has this problem to solve. So maybe camunda can build a recommended example?

Greetings
Dennis