Skip to content

Commit 485f28e

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 <zhouyanming@gmail.com>
1 parent b9b08bf commit 485f28e

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

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

Lines changed: 28 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;
@@ -44,6 +43,7 @@
4443
import org.springframework.beans.factory.InitializingBean;
4544
import org.springframework.dao.OptimisticLockingFailureException;
4645
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
46+
import org.springframework.jdbc.core.PreparedStatementCallback;
4747
import org.springframework.jdbc.core.RowMapper;
4848
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
4949
import org.springframework.lang.Nullable;
@@ -102,6 +102,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
102102
FROM %PREFIX%JOB_EXECUTION JE
103103
JOIN %PREFIX%STEP_EXECUTION SE ON SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID
104104
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
105+
ORDER BY SE.CREATE_TIME DESC, SE.STEP_EXECUTION_ID DESC
105106
""";
106107

107108
private static final String CURRENT_VERSION_STEP_EXECUTION = """
@@ -121,10 +122,6 @@ SELECT COUNT(*)
121122
WHERE STEP_EXECUTION_ID = ? and VERSION = ?
122123
""";
123124

124-
private static final Comparator<StepExecution> BY_CREATE_TIME_DESC_ID_DESC = Comparator
125-
.comparing(StepExecution::getCreateTime, Comparator.reverseOrder())
126-
.thenComparing(StepExecution::getId, Comparator.reverseOrder());
127-
128125
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
129126

130127
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
@@ -333,27 +330,34 @@ public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecut
333330
}
334331
}
335332

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

359363
@Override

0 commit comments

Comments
 (0)