Skip to content

Commit 0c8848e

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 0d3832f commit 0c8848e

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616

1717
package org.springframework.batch.core.repository.dao.jdbc;
1818

19-
import java.sql.PreparedStatement;
20-
import java.sql.SQLException;
19+
import java.sql.ResultSet;
2120
import java.sql.Timestamp;
2221
import java.sql.Types;
2322
import java.util.ArrayList;
2423
import java.util.Arrays;
25-
import java.util.Collection;
26-
import java.util.Comparator;
27-
import java.util.Iterator;
2824
import java.util.List;
2925
import java.util.concurrent.locks.Lock;
3026
import java.util.concurrent.locks.ReentrantLock;
@@ -41,7 +37,7 @@
4137
import org.springframework.batch.core.repository.dao.StepExecutionDao;
4238
import org.springframework.beans.factory.InitializingBean;
4339
import org.springframework.dao.OptimisticLockingFailureException;
44-
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
40+
import org.springframework.jdbc.core.PreparedStatementCallback;
4541
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
4642
import org.springframework.lang.Nullable;
4743
import org.springframework.util.Assert;
@@ -99,6 +95,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
9995
FROM %PREFIX%JOB_EXECUTION JE
10096
JOIN %PREFIX%STEP_EXECUTION SE ON SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID
10197
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
98+
ORDER BY SE.CREATE_TIME DESC, SE.STEP_EXECUTION_ID DESC
10299
""";
103100

104101
private static final String CURRENT_VERSION_STEP_EXECUTION = """
@@ -124,10 +121,6 @@ SELECT COUNT(*)
124121
WHERE SE.STEP_EXECUTION_ID = ? AND JE.JOB_EXECUTION_ID = SE.JOB_EXECUTION_ID
125122
""";
126123

127-
private static final Comparator<StepExecution> BY_CREATE_TIME_DESC_ID_DESC = Comparator
128-
.comparing(StepExecution::getCreateTime, Comparator.reverseOrder())
129-
.thenComparing(StepExecution::getId, Comparator.reverseOrder());
130-
131124
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
132125

133126
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
@@ -318,28 +311,35 @@ public StepExecution getStepExecution(JobExecution jobExecution, long stepExecut
318311
}
319312
}
320313

314+
@Nullable
321315
@Override
322316
public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) {
323-
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_LAST_STEP_EXECUTION), (rs, rowNum) -> {
324-
long jobExecutionId = rs.getLong(19);
325-
JobExecution jobExecution = new JobExecution(jobExecutionId, jobInstance,
326-
jobExecutionDao.getJobParameters(jobExecutionId));
327-
jobExecution.setStartTime(rs.getTimestamp(20) == null ? null : rs.getTimestamp(20).toLocalDateTime());
328-
jobExecution.setEndTime(rs.getTimestamp(21) == null ? null : rs.getTimestamp(21).toLocalDateTime());
329-
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(22)));
330-
jobExecution.setExitStatus(new ExitStatus(rs.getString(23), rs.getString(24)));
331-
jobExecution.setCreateTime(rs.getTimestamp(25) == null ? null : rs.getTimestamp(25).toLocalDateTime());
332-
jobExecution.setLastUpdated(rs.getTimestamp(26) == null ? null : rs.getTimestamp(26).toLocalDateTime());
333-
jobExecution.setVersion(rs.getInt(27));
334-
return new StepExecutionRowMapper(jobExecution).mapRow(rs, rowNum);
335-
}, jobInstance.getInstanceId(), stepName);
336-
executions.sort(BY_CREATE_TIME_DESC_ID_DESC);
337-
if (executions.isEmpty()) {
338-
return null;
339-
}
340-
else {
341-
return executions.get(0);
342-
}
317+
return getJdbcTemplate().execute(getQuery(GET_LAST_STEP_EXECUTION),
318+
(PreparedStatementCallback<StepExecution>) statement -> {
319+
statement.setMaxRows(1);
320+
statement.setLong(1, jobInstance.getInstanceId());
321+
statement.setString(2, stepName);
322+
try (ResultSet rs = statement.executeQuery()) {
323+
if (rs.next()) {
324+
Long jobExecutionId = rs.getLong(19);
325+
JobExecution jobExecution = new JobExecution(jobExecutionId, jobInstance,
326+
jobExecutionDao.getJobParameters(jobExecutionId));
327+
jobExecution.setStartTime(
328+
rs.getTimestamp(20) == null ? null : rs.getTimestamp(20).toLocalDateTime());
329+
jobExecution
330+
.setEndTime(rs.getTimestamp(21) == null ? null : rs.getTimestamp(21).toLocalDateTime());
331+
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(22)));
332+
jobExecution.setExitStatus(new ExitStatus(rs.getString(23), rs.getString(24)));
333+
jobExecution.setCreateTime(
334+
rs.getTimestamp(25) == null ? null : rs.getTimestamp(25).toLocalDateTime());
335+
jobExecution.setLastUpdated(
336+
rs.getTimestamp(26) == null ? null : rs.getTimestamp(26).toLocalDateTime());
337+
jobExecution.setVersion(rs.getInt(27));
338+
return new StepExecutionRowMapper(jobExecution).mapRow(rs, 0);
339+
}
340+
return null;
341+
}
342+
});
343343
}
344344

345345
/**

0 commit comments

Comments
 (0)