Skip to content

Commit 5c92623

Browse files
committed
Fix Timestamp to LocalDateTime conversion in Job/Step execution DAOs
Persisting a LocalDateTime as a Timestamp with `Timestamp.valueOf(LocalDateTime)` and retrieving it with `ResultSet#getObject(index, LocalDateTime.class)` seems to result in a data loss at the millisecond fraction level. This commit changes the usage of `ResultSet#getObject(index, LocalDateTime.class)` to `ResultSet#getTimestamp(index).toLocalDateTime()`. A round trip to the database with `Timestamp.valueOf(LocalDateTime)` and `Timestamp.toLocalDateTime()` should not result in a data loss.
1 parent 220de61 commit 5c92623

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,12 @@ public JobExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
447447
jobExecution = new JobExecution(jobInstance, id, jobParameters);
448448
}
449449

450-
jobExecution.setStartTime(rs.getObject(2, LocalDateTime.class));
451-
jobExecution.setEndTime(rs.getObject(3, LocalDateTime.class));
450+
jobExecution.setStartTime(rs.getTimestamp(2) == null ? null : rs.getTimestamp(2).toLocalDateTime());
451+
jobExecution.setEndTime(rs.getTimestamp(3) == null ? null : rs.getTimestamp(3).toLocalDateTime());
452452
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(4)));
453453
jobExecution.setExitStatus(new ExitStatus(rs.getString(5), rs.getString(6)));
454-
jobExecution.setCreateTime(rs.getObject(7, LocalDateTime.class));
455-
jobExecution.setLastUpdated(rs.getObject(8, LocalDateTime.class));
454+
jobExecution.setCreateTime(rs.getTimestamp(7) == null ? null : rs.getTimestamp(7).toLocalDateTime());
455+
jobExecution.setLastUpdated(rs.getTimestamp(8) == null ? null : rs.getTimestamp(8).toLocalDateTime());
456456
jobExecution.setVersion(rs.getInt(9));
457457
return jobExecution;
458458
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,12 @@ public StepExecution getLastStepExecution(JobInstance jobInstance, String stepNa
334334
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_LAST_STEP_EXECUTION), (rs, rowNum) -> {
335335
Long jobExecutionId = rs.getLong(19);
336336
JobExecution jobExecution = new JobExecution(jobExecutionId);
337-
jobExecution.setStartTime(rs.getObject(20, LocalDateTime.class));
338-
jobExecution.setEndTime(rs.getObject(21, LocalDateTime.class));
337+
jobExecution.setStartTime(rs.getTimestamp(20) == null ? null : rs.getTimestamp(20).toLocalDateTime());
338+
jobExecution.setEndTime(rs.getTimestamp(21) == null ? null : rs.getTimestamp(21).toLocalDateTime());
339339
jobExecution.setStatus(BatchStatus.valueOf(rs.getString(22)));
340340
jobExecution.setExitStatus(new ExitStatus(rs.getString(23), rs.getString(24)));
341-
jobExecution.setCreateTime(rs.getObject(25, LocalDateTime.class));
342-
jobExecution.setLastUpdated(rs.getObject(26, LocalDateTime.class));
341+
jobExecution.setCreateTime(rs.getTimestamp(25) == null ? null : rs.getTimestamp(25).toLocalDateTime());
342+
jobExecution.setLastUpdated(rs.getTimestamp(26) == null ? null : rs.getTimestamp(26).toLocalDateTime());
343343
jobExecution.setVersion(rs.getInt(27));
344344
return new StepExecutionRowMapper(jobExecution).mapRow(rs, rowNum);
345345
}, jobInstance.getInstanceId(), stepName);
@@ -382,8 +382,8 @@ public StepExecutionRowMapper(JobExecution jobExecution) {
382382
@Override
383383
public StepExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
384384
StepExecution stepExecution = new StepExecution(rs.getString(2), jobExecution, rs.getLong(1));
385-
stepExecution.setStartTime(rs.getObject(3, LocalDateTime.class));
386-
stepExecution.setEndTime(rs.getObject(4, LocalDateTime.class));
385+
stepExecution.setStartTime(rs.getTimestamp(3) == null ? null : rs.getTimestamp(3).toLocalDateTime());
386+
stepExecution.setEndTime(rs.getTimestamp(4) == null ? null : rs.getTimestamp(4).toLocalDateTime());
387387
stepExecution.setStatus(BatchStatus.valueOf(rs.getString(5)));
388388
stepExecution.setCommitCount(rs.getInt(6));
389389
stepExecution.setReadCount(rs.getInt(7));
@@ -394,9 +394,9 @@ public StepExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
394394
stepExecution.setWriteSkipCount(rs.getInt(13));
395395
stepExecution.setProcessSkipCount(rs.getInt(14));
396396
stepExecution.setRollbackCount(rs.getInt(15));
397-
stepExecution.setLastUpdated(rs.getObject(16, LocalDateTime.class));
397+
stepExecution.setLastUpdated(rs.getTimestamp(16) == null ? null : rs.getTimestamp(16).toLocalDateTime());
398398
stepExecution.setVersion(rs.getInt(17));
399-
stepExecution.setCreateTime(rs.getObject(18, LocalDateTime.class));
399+
stepExecution.setCreateTime(rs.getTimestamp(18) == null ? null : rs.getTimestamp(18).toLocalDateTime());
400400
return stepExecution;
401401
}
402402

0 commit comments

Comments
 (0)