Skip to content

Commit 85ff8a7

Browse files
committed
Improve JdbcStepExecutionDao::getLastStepExecution to reuse JdbcJobExecutionDao::getJobExecution
This commit eliminate duplicate `StepExecution` mapping code from `JdbcStepExecutionDao`, make the code cleaner and keep the consistent style. The drawback is it requires one more query, but it's acceptable since the query is by primary key. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 5750492 commit 85ff8a7

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
3030

31-
import org.springframework.batch.core.BatchStatus;
32-
import org.springframework.batch.core.ExitStatus;
3331
import org.springframework.batch.core.job.JobExecution;
3432
import org.springframework.batch.core.job.JobInstance;
3533
import org.springframework.batch.core.step.StepExecution;
@@ -91,7 +89,7 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
9189
private static final String GET_STEP_EXECUTION = GET_RAW_STEP_EXECUTIONS + " WHERE STEP_EXECUTION_ID = ?";
9290

9391
private static final String GET_LAST_STEP_EXECUTION = """
94-
SELECT SE.STEP_EXECUTION_ID, SE.STEP_NAME, SE.START_TIME, SE.END_TIME, SE.STATUS, SE.COMMIT_COUNT, SE.READ_COUNT, SE.FILTER_COUNT, SE.WRITE_COUNT, SE.EXIT_CODE, SE.EXIT_MESSAGE, SE.READ_SKIP_COUNT, SE.WRITE_SKIP_COUNT, SE.PROCESS_SKIP_COUNT, SE.ROLLBACK_COUNT, SE.LAST_UPDATED, SE.VERSION, SE.CREATE_TIME, JE.JOB_EXECUTION_ID, JE.START_TIME, JE.END_TIME, JE.STATUS, JE.EXIT_CODE, JE.EXIT_MESSAGE, JE.CREATE_TIME, JE.LAST_UPDATED, JE.VERSION
92+
SELECT SE.STEP_EXECUTION_ID, SE.STEP_NAME, SE.JOB_EXECUTION_ID, SE.START_TIME, SE.END_TIME, SE.STATUS, SE.COMMIT_COUNT, SE.READ_COUNT, SE.FILTER_COUNT, SE.WRITE_COUNT, SE.EXIT_CODE, SE.EXIT_MESSAGE, SE.READ_SKIP_COUNT, SE.WRITE_SKIP_COUNT, SE.PROCESS_SKIP_COUNT, SE.ROLLBACK_COUNT, SE.LAST_UPDATED, SE.VERSION, SE.CREATE_TIME
9593
FROM %PREFIX%JOB_EXECUTION JE
9694
JOIN %PREFIX%STEP_EXECUTION SE ON SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID
9795
WHERE JE.JOB_INSTANCE_ID = ? AND SE.STEP_NAME = ?
@@ -320,21 +318,9 @@ public StepExecution getLastStepExecution(JobInstance jobInstance, String stepNa
320318
statement.setString(2, stepName);
321319
try (ResultSet rs = statement.executeQuery()) {
322320
if (rs.next()) {
323-
Long jobExecutionId = rs.getLong(19);
324-
JobExecution jobExecution = new JobExecution(jobExecutionId, jobInstance,
325-
jobExecutionDao.getJobParameters(jobExecutionId));
326-
jobExecution.setStartTime(
327-
rs.getTimestamp(20) == null ? null : rs.getTimestamp(20).toLocalDateTime());
328-
jobExecution
329-
.setEndTime(rs.getTimestamp(21) == null ? null : rs.getTimestamp(21).toLocalDateTime());
330-
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(22)));
331-
jobExecution.setExitStatus(new ExitStatus(rs.getString(23), rs.getString(24)));
332-
jobExecution.setCreateTime(
333-
rs.getTimestamp(25) == null ? null : rs.getTimestamp(25).toLocalDateTime());
334-
jobExecution.setLastUpdated(
335-
rs.getTimestamp(26) == null ? null : rs.getTimestamp(26).toLocalDateTime());
336-
jobExecution.setVersion(rs.getInt(27));
337-
return new StepExecutionRowMapper(jobExecution).mapRow(rs, 0);
321+
return new StepExecutionRowMapper(
322+
jobExecutionDao.getJobExecution(rs.getLong("JOB_EXECUTION_ID")))
323+
.mapRow(rs, 0);
338324
}
339325
return null;
340326
}

spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/jdbc/JdbcStepExecutionDaoTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,21 @@ void testDeleteStepExecution() {
158158
Assertions.assertEquals(0, JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STEP_EXECUTION"));
159159
}
160160

161+
@Test
162+
void testGetLastStepExecution() {
163+
// Given
164+
JobParameters jobParameters = new JobParameters();
165+
JobInstance jobInstance = jdbcJobInstanceDao.createJobInstance("job", jobParameters);
166+
JobExecution jobExecution = jdbcJobExecutionDao.createJobExecution(jobInstance, jobParameters);
167+
jdbcStepExecutionDao.createStepExecution("step1", jobExecution);
168+
jdbcStepExecutionDao.createStepExecution("step2", jobExecution);
169+
StepExecution stepExecution = jdbcStepExecutionDao.createStepExecution("step2", jobExecution);
170+
171+
// when
172+
StepExecution lastStepExecution = jdbcStepExecutionDao.getLastStepExecution(jobInstance, "step2");
173+
174+
// Then
175+
assertEquals(stepExecution, lastStepExecution);
176+
}
177+
161178
}

0 commit comments

Comments
 (0)