Skip to content

Commit c365356

Browse files
committed
Optimize findJobExecutions to reuse provided JobInstance
1 parent 2e9755d commit c365356

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/jdbc/JdbcJobExecutionDao.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,17 @@ public List<JobExecution> findJobExecutions(final JobInstance jobInstance) {
218218

219219
Assert.notNull(jobInstance, "Job instance cannot be null.");
220220
long jobInstanceId = jobInstance.getId();
221-
// TODO optimize to a single query with a join if possible
222-
List<Long> jobExecutionIdsSortedBackwardByCreationOrder = getJdbcTemplate()
223-
.queryForList(getQuery(GET_JOB_EXECUTION_IDS_BY_INSTANCE_ID), Long.class, jobInstanceId);
224-
List<JobExecution> jobExecutions = new ArrayList<>(jobExecutionIdsSortedBackwardByCreationOrder.size());
225-
for (Long jobExecutionId : jobExecutionIdsSortedBackwardByCreationOrder) {
226-
jobExecutions.add(getJobExecution(jobExecutionId));
221+
222+
// TODO Further reduce database round-trips by retrieving executions and parameters in fewer queries.
223+
List<Long> executionIds = getJdbcTemplate()
224+
.queryForList(getQuery(GET_JOB_EXECUTION_IDS_BY_INSTANCE_ID), Long.class, jobInstanceId);
225+
226+
List<JobExecution> jobExecutions = new ArrayList<>(executionIds.size());
227+
for (Long executionId : executionIds) {
228+
JobExecution jobExecution = mapJobExecution(executionId, jobInstance);
229+
if (jobExecution != null) {
230+
jobExecutions.add(jobExecution);
231+
}
227232
}
228233
return jobExecutions;
229234
}
@@ -336,6 +341,17 @@ private long getJobInstanceId(long jobExecutionId) {
336341
jobExecutionId);
337342
}
338343

344+
private JobExecution mapJobExecution(long executionId, JobInstance jobInstance) {
345+
JobParameters jobParameters = getJobParameters(executionId);
346+
try {
347+
return getJdbcTemplate().queryForObject(getQuery(GET_EXECUTION_BY_ID),
348+
new JobExecutionRowMapper(jobInstance, jobParameters), executionId);
349+
}
350+
catch (EmptyResultDataAccessException ex) {
351+
return null;
352+
}
353+
}
354+
339355
@Override
340356
public Set<JobExecution> findRunningJobExecutions(String jobName) {
341357
return getJdbcTemplate().queryForList(getQuery(GET_RUNNING_EXECUTION_FOR_INSTANCE), Long.class, jobName)

0 commit comments

Comments
 (0)