Skip to content

Commit e75a988

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 08c4cb1 commit e75a988

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/jdbc/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;
@@ -43,6 +42,7 @@
4342
import org.springframework.beans.factory.InitializingBean;
4443
import org.springframework.dao.OptimisticLockingFailureException;
4544
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
45+
import org.springframework.jdbc.core.PreparedStatementCallback;
4646
import org.springframework.jdbc.core.RowMapper;
4747
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
4848
import org.springframework.lang.Nullable;
@@ -68,6 +68,7 @@
6868
* @author Mahmoud Ben Hassine
6969
* @author Baris Cubukcuoglu
7070
* @author Minsoo Kim
71+
* @author Yanming Zhou
7172
* @see StepExecutionDao
7273
*/
7374
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -100,6 +101,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
100101
FROM %PREFIX%JOB_EXECUTION JE
101102
JOIN %PREFIX%STEP_EXECUTION SE ON SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID
102103
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
104+
ORDER BY SE.CREATE_TIME DESC, SE.JOB_EXECUTION_ID DESC
103105
""";
104106

105107
private static final String CURRENT_VERSION_STEP_EXECUTION = """
@@ -119,10 +121,6 @@ SELECT COUNT(*)
119121
WHERE STEP_EXECUTION_ID = ?
120122
""";
121123

122-
private static final Comparator<StepExecution> BY_CREATE_TIME_DESC_ID_DESC = Comparator
123-
.comparing(StepExecution::getCreateTime, Comparator.reverseOrder())
124-
.thenComparing(StepExecution::getId, Comparator.reverseOrder());
125-
126124
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
127125

128126
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
@@ -339,27 +337,34 @@ public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecut
339337
}
340338
}
341339

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

365370
@Override

0 commit comments

Comments
 (0)