Skip to content

Commit 6372260

Browse files
parikshitduttafmbenhassine
authored andcommitted
Change default value of DATE_VAL to null for non-date type job parameters
Resolves #1577
1 parent 9f4127f commit 6372260

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.sql.ResultSet;
2020
import java.sql.SQLException;
21-
import java.sql.Timestamp;
2221
import java.sql.Types;
2322
import java.util.HashMap;
2423
import java.util.HashSet;
@@ -344,13 +343,13 @@ private void insertParameter(Long executionId, ParameterType type, String key,
344343
String identifyingFlag = identifying? "Y":"N";
345344

346345
if (type == ParameterType.STRING) {
347-
args = new Object[] { executionId, key, type, value, new Timestamp(0L),
346+
args = new Object[] { executionId, key, type, value, null,
348347
0L, 0D, identifyingFlag};
349348
} else if (type == ParameterType.LONG) {
350-
args = new Object[] { executionId, key, type, "", new Timestamp(0L),
349+
args = new Object[] { executionId, key, type, "", null,
351350
value, 0.0d, identifyingFlag};
352351
} else if (type == ParameterType.DOUBLE) {
353-
args = new Object[] { executionId, key, type, "", new Timestamp(0L), 0L,
352+
args = new Object[] { executionId, key, type, "", null, 0L,
354353
value, identifyingFlag};
355354
} else if (type == ParameterType.DATE) {
356355
args = new Object[] { executionId, key, type, "", value, 0L, 0D, identifyingFlag};

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2013 the original author or authors.
2+
* Copyright 2008-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,15 +15,33 @@
1515
*/
1616
package org.springframework.batch.core.repository.dao;
1717

18+
import java.util.Date;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
1823
import javax.sql.DataSource;
1924

25+
import static org.junit.Assert.assertNull;
26+
import org.junit.Test;
2027
import org.junit.runner.RunWith;
28+
29+
import org.springframework.batch.core.JobExecution;
30+
import org.springframework.batch.core.JobParameter;
31+
import org.springframework.batch.core.JobParameters;
2132
import org.springframework.beans.factory.annotation.Autowired;
2233
import org.springframework.jdbc.core.JdbcTemplate;
34+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
35+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
36+
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
2337
import org.springframework.test.context.ContextConfiguration;
2438
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2539
import org.springframework.test.jdbc.JdbcTestUtils;
40+
import org.springframework.transaction.annotation.Transactional;
2641

42+
/**
43+
* @author Parikshit Dutta
44+
*/
2745
@RunWith(SpringJUnit4ClassRunner.class)
2846
@ContextConfiguration(locations = { "sql-dao-test.xml" })
2947
public class JdbcJobExecutionDaoTests extends AbstractJobExecutionDaoTests {
@@ -62,4 +80,35 @@ protected StepExecutionDao getStepExecutionDao() {
6280
return stepExecutionDao;
6381
}
6482

83+
@Transactional
84+
@Test
85+
public void testSavedDateIsNullForNonDateTypeJobParams() {
86+
final String FIND_DATE_PARAM_FROM_ID = "SELECT DATE_VAL " +
87+
"from %PREFIX%JOB_EXECUTION_PARAMS where JOB_EXECUTION_ID = :JOB_EXECUTION_ID";
88+
89+
Map<String,JobParameter> parameters = new HashMap<>();
90+
parameters.put("string-param", new JobParameter("value"));
91+
parameters.put("long-param", new JobParameter(1L));
92+
parameters.put("double-param", new JobParameter(1D));
93+
94+
JobExecution execution = new JobExecution(jobInstance, new JobParameters(parameters));
95+
dao.saveJobExecution(execution);
96+
97+
List<JobExecution> executions = dao.findJobExecutions(jobInstance);
98+
JobExecution savedJobExecution = executions.get(0);
99+
100+
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(
101+
jdbcTemplate.getDataSource());
102+
103+
JdbcJobExecutionDao jdbcJobExecutionDao = (JdbcJobExecutionDao) jobExecutionDao;
104+
String query = jdbcJobExecutionDao.getQuery(FIND_DATE_PARAM_FROM_ID);
105+
106+
SqlParameterSource namedParameters = new MapSqlParameterSource()
107+
.addValue("JOB_EXECUTION_ID", savedJobExecution.getJobId());
108+
109+
List<Date> paramValues = namedParameterJdbcTemplate.queryForList(query, namedParameters, Date.class);
110+
for (Date paramValue: paramValues) {
111+
assertNull(paramValue);
112+
}
113+
}
65114
}

0 commit comments

Comments
 (0)