Skip to content

Commit f9cff16

Browse files
authored
Improve retrieval of running job executions (#5131)
This commit could save one unnecessary query. See 5750492 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 298da2a commit f9cff16

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Set;
2727
import java.util.concurrent.locks.Lock;
2828
import java.util.concurrent.locks.ReentrantLock;
29+
import java.util.stream.Collectors;
2930

3031
import org.apache.commons.logging.Log;
3132
import org.apache.commons.logging.LogFactory;
@@ -106,7 +107,7 @@ SELECT COUNT(*)
106107
private static final String GET_RUNNING_EXECUTION_FOR_INSTANCE = """
107108
SELECT E.JOB_EXECUTION_ID
108109
FROM %PREFIX%JOB_EXECUTION E, %PREFIX%JOB_INSTANCE I
109-
WHERE E.JOB_INSTANCE_ID=I.JOB_INSTANCE_ID AND I.JOB_INSTANCE_ID=? AND E.STATUS IN ('STARTING', 'STARTED', 'STOPPING')
110+
WHERE E.JOB_INSTANCE_ID=I.JOB_INSTANCE_ID AND I.JOB_NAME=? AND E.STATUS IN ('STARTING', 'STARTED', 'STOPPING')
110111
""";
111112

112113
private static final String CURRENT_VERSION_JOB_EXECUTION = """
@@ -340,21 +341,10 @@ private long getJobInstanceId(long jobExecutionId) {
340341

341342
@Override
342343
public Set<JobExecution> findRunningJobExecutions(String jobName) {
343-
final Set<JobExecution> result = new HashSet<>();
344-
List<Long> jobInstanceIds = this.jobInstanceDao.getJobInstanceIds(jobName);
345-
for (long jobInstanceId : jobInstanceIds) {
346-
List<Long> runningJobExecutionIds = getJdbcTemplate()
347-
.queryForList(getQuery(GET_RUNNING_EXECUTION_FOR_INSTANCE), Long.class, jobInstanceId);
348-
if (runningJobExecutionIds.isEmpty()) {
349-
continue;
350-
}
351-
// There should be only one running execution per job instance, enforced at
352-
// startup time
353-
Long jobExecutionId = runningJobExecutionIds.get(0);
354-
JobExecution runningJobExecution = getJobExecution(jobExecutionId);
355-
result.add(runningJobExecution);
356-
}
357-
return result;
344+
return getJdbcTemplate().queryForList(getQuery(GET_RUNNING_EXECUTION_FOR_INSTANCE), Long.class, jobName)
345+
.stream()
346+
.map(this::getJobExecution)
347+
.collect(Collectors.toSet());
358348
}
359349

360350
@Override

0 commit comments

Comments
 (0)