Querying tasks causes HistoricTaskInstanceEntity to be loaded for each result

Description

Querying tasks causes HistoricTaskInstanceEntity to be loaded for each result. So when a TaskQuery.list() results in 100 Tasks, 100 queries to select historic task instances are performed while myBatis is converting sql resultset in TaskEntities.

MyBatis uses the TaskEntity setters, which are rigged to update the corresponding field in the HistoricTaskInstance (when commandContext is active, which is obviously the case when performing query):

public void setName(String name) {
this.name = name;

CommandContext commandContext = Context.getCommandContext();
// if there is no command context, then it means that the user is calling the
// setAssignee outside a service method. E.g. while creating a new task.
if (commandContext!=null) {
int historyLevel = Context.getProcessEngineConfiguration().getHistoryLevel();
if (historyLevel >= ProcessEngineConfigurationImpl.HISTORYLEVEL_AUDIT) {
HistoricTaskInstanceEntity historicTaskInstance = commandContext.getDbSqlSession().selectById(HistoricTaskInstanceEntity.class, id);
if (historicTaskInstance!=null) {
historicTaskInstance.setName(name);
}
}
}
}

Solution would be to have myBatis use different setter methods than the regular setName() etc, exposed in DelegateTask.

Environment

None

Assignee

Tom Baeyens

Reporter

Frederik Heremans

Labels

None

Fix versions

Priority

Critical
Configure