Skip to content

Commit 57900b5

Browse files
committed
Improve "JdbcJobExecutionDao#synchronizeStatus(StepExecution)"
This commit will save one query to improve performance. Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent d8632a3 commit 57900b5

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.HashSet;
2525
import java.util.List;
26+
import java.util.Objects;
2627
import java.util.Set;
2728
import java.util.concurrent.locks.Lock;
2829
import java.util.concurrent.locks.ReentrantLock;
@@ -79,8 +80,8 @@ SELECT COUNT(*)
7980
WHERE JOB_EXECUTION_ID = ?
8081
""";
8182

82-
private static final String GET_STATUS = """
83-
SELECT STATUS
83+
private static final String GET_VERSION_AND_STATUS = """
84+
SELECT VERSION, STATUS
8485
FROM %PREFIX%JOB_EXECUTION
8586
WHERE JOB_EXECUTION_ID = ?
8687
""";
@@ -346,14 +347,13 @@ public Set<JobExecution> findRunningJobExecutions(String jobName) {
346347

347348
@Override
348349
public void synchronizeStatus(JobExecution jobExecution) {
349-
int currentVersion = getJdbcTemplate().queryForObject(getQuery(CURRENT_VERSION_JOB_EXECUTION), Integer.class,
350-
jobExecution.getId());
351-
352-
if (currentVersion != jobExecution.getVersion()) {
353-
String status = getJdbcTemplate().queryForObject(getQuery(GET_STATUS), String.class, jobExecution.getId());
354-
jobExecution.upgradeStatus(BatchStatus.valueOf(status));
355-
jobExecution.setVersion(currentVersion);
356-
}
350+
getJdbcTemplate().query(getQuery(GET_VERSION_AND_STATUS), rs -> {
351+
Integer currentVersion = rs.getInt("VERSION");
352+
if (!Objects.equals(currentVersion, jobExecution.getVersion())) {
353+
jobExecution.upgradeStatus(BatchStatus.valueOf(rs.getString("STATUS")));
354+
jobExecution.setVersion(currentVersion);
355+
}
356+
}, jobExecution.getId());
357357
}
358358

359359
/**

0 commit comments

Comments
 (0)