Skip to content

Commit 00b0126

Browse files
committed
Improve performance of JdbcStepExecutionDao::getLastStepExecution
1. Use SQL order by clause instead of Java Comparator 2. Limit result set size to 1 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 2bd5b84 commit 00b0126

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.Collection;
27-
import java.util.Comparator;
2827
import java.util.Iterator;
2928
import java.util.List;
3029
import java.util.concurrent.locks.Lock;
@@ -41,6 +40,7 @@
4140
import org.springframework.beans.factory.InitializingBean;
4241
import org.springframework.dao.OptimisticLockingFailureException;
4342
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
43+
import org.springframework.jdbc.core.PreparedStatementCallback;
4444
import org.springframework.jdbc.core.RowMapper;
4545
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
4646
import org.springframework.lang.Nullable;
@@ -66,6 +66,7 @@
6666
* @author Mahmoud Ben Hassine
6767
* @author Baris Cubukcuoglu
6868
* @author Minsoo Kim
69+
* @author Yanming Zhou
6970
* @see StepExecutionDao
7071
*/
7172
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -98,6 +99,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
9899
FROM %PREFIX%JOB_EXECUTION JE
99100
JOIN %PREFIX%STEP_EXECUTION SE ON SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID
100101
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
102+
ORDER BY SE.CREATE_TIME DESC, SE.JOB_EXECUTION_ID DESC
101103
""";
102104

103105
private static final String CURRENT_VERSION_STEP_EXECUTION = """
@@ -117,10 +119,6 @@ SELECT COUNT(*)
117119
WHERE STEP_EXECUTION_ID = ?
118120
""";
119121

120-
private static final Comparator<StepExecution> BY_CREATE_TIME_DESC_ID_DESC = Comparator
121-
.comparing(StepExecution::getCreateTime, Comparator.reverseOrder())
122-
.thenComparing(StepExecution::getId, Comparator.reverseOrder());
123-
124122
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
125123

126124
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
@@ -337,27 +335,34 @@ public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecut
337335
}
338336
}
339337

338+
@Nullable
340339
@Override
341340
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {
342-
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_LAST_STEP_EXECUTION), (rs, rowNum) -> {
343-
Long jobExecutionId = rs.getLong(19);
344-
JobExecution jobExecution = new JobExecution(jobExecutionId);
345-
jobExecution.setStartTime(rs.getTimestamp(20) == null ? null : rs.getTimestamp(20).toLocalDateTime());
346-
jobExecution.setEndTime(rs.getTimestamp(21) == null ? null : rs.getTimestamp(21).toLocalDateTime());
347-
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(22)));
348-
jobExecution.setExitStatus(new ExitStatus(rs.getString(23), rs.getString(24)));
349-
jobExecution.setCreateTime(rs.getTimestamp(25) == null ? null : rs.getTimestamp(25).toLocalDateTime());
350-
jobExecution.setLastUpdated(rs.getTimestamp(26) == null ? null : rs.getTimestamp(26).toLocalDateTime());
351-
jobExecution.setVersion(rs.getInt(27));
352-
return new StepExecutionRowMapper(jobExecution).mapRow(rs, rowNum);
353-
}, jobInstance.getInstanceId(), stepName);
354-
executions.sort(BY_CREATE_TIME_DESC_ID_DESC);
355-
if (executions.isEmpty()) {
356-
return null;
357-
}
358-
else {
359-
return executions.get(0);
360-
}
341+
return getJdbcTemplate().execute(getQuery(GET_LAST_STEP_EXECUTION),
342+
(PreparedStatementCallback<StepExecution>) statement -> {
343+
statement.setMaxRows(1);
344+
statement.setLong(1, jobInstance.getInstanceId());
345+
statement.setString(2, stepName);
346+
try (ResultSet rs = statement.executeQuery()) {
347+
if (rs.next()) {
348+
Long jobExecutionId = rs.getLong(19);
349+
JobExecution jobExecution = new JobExecution(jobExecutionId);
350+
jobExecution.setStartTime(
351+
rs.getTimestamp(20) == null ? null : rs.getTimestamp(20).toLocalDateTime());
352+
jobExecution
353+
.setEndTime(rs.getTimestamp(21) == null ? null : rs.getTimestamp(21).toLocalDateTime());
354+
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(22)));
355+
jobExecution.setExitStatus(new ExitStatus(rs.getString(23), rs.getString(24)));
356+
jobExecution.setCreateTime(
357+
rs.getTimestamp(25) == null ? null : rs.getTimestamp(25).toLocalDateTime());
358+
jobExecution.setLastUpdated(
359+
rs.getTimestamp(26) == null ? null : rs.getTimestamp(26).toLocalDateTime());
360+
jobExecution.setVersion(rs.getInt(27));
361+
return new StepExecutionRowMapper(jobExecution).mapRow(rs, 0);
362+
}
363+
return null;
364+
}
365+
});
361366
}
362367

363368
@Override

0 commit comments

Comments
 (0)