Skip to content

Commit 00ef86b

Browse files
committed
Use jdbcTemplate.queryForStream().findFirst()
Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 2bd5b84 commit 00ef86b

File tree

4 files changed

+24
-48
lines changed

4 files changed

+24
-48
lines changed

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import java.util.Collection;
2828
import java.util.HashMap;
2929
import java.util.Iterator;
30-
import java.util.List;
3130
import java.util.Map;
3231
import java.util.Map.Entry;
3332
import java.util.concurrent.locks.Lock;
3433
import java.util.concurrent.locks.ReentrantLock;
34+
import java.util.stream.Stream;
3535

3636
import org.springframework.batch.core.JobExecution;
3737
import org.springframework.batch.core.StepExecution;
@@ -57,6 +57,7 @@
5757
* @author Michael Minella
5858
* @author David Turanski
5959
* @author Mahmoud Ben Hassine
60+
* @author Yanming Zhou
6061
*/
6162
public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao {
6263

@@ -154,13 +155,9 @@ public ExecutionContext getExecutionContext(JobExecution jobExecution) {
154155
Long executionId = jobExecution.getId();
155156
Assert.notNull(executionId, "ExecutionId must not be null.");
156157

157-
List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_JOB_EXECUTION_CONTEXT),
158-
new ExecutionContextRowMapper(), executionId);
159-
if (!results.isEmpty()) {
160-
return results.get(0);
161-
}
162-
else {
163-
return new ExecutionContext();
158+
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_JOB_EXECUTION_CONTEXT),
159+
new ExecutionContextRowMapper(), executionId)) {
160+
return stream.findFirst().orElseGet(ExecutionContext::new);
164161
}
165162
}
166163

@@ -169,13 +166,9 @@ public ExecutionContext getExecutionContext(StepExecution stepExecution) {
169166
Long executionId = stepExecution.getId();
170167
Assert.notNull(executionId, "ExecutionId must not be null.");
171168

172-
List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_STEP_EXECUTION_CONTEXT),
173-
new ExecutionContextRowMapper(), executionId);
174-
if (results.size() > 0) {
175-
return results.get(0);
176-
}
177-
else {
178-
return new ExecutionContext();
169+
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_STEP_EXECUTION_CONTEXT),
170+
new ExecutionContextRowMapper(), executionId)) {
171+
return stream.findFirst().orElseGet(ExecutionContext::new);
179172
}
180173
}
181174

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Set;
2929
import java.util.concurrent.locks.Lock;
3030
import java.util.concurrent.locks.ReentrantLock;
31+
import java.util.stream.Stream;
3132

3233
import org.apache.commons.logging.Log;
3334
import org.apache.commons.logging.LogFactory;
@@ -74,6 +75,7 @@
7475
* @author Dimitrios Liapis
7576
* @author Philippe Marschall
7677
* @author Jinwoo Bae
78+
* @author Yanming Zhou
7779
*/
7880
public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean {
7981

@@ -339,16 +341,9 @@ public JobExecution getLastJobExecution(JobInstance jobInstance) {
339341

340342
Long id = jobInstance.getId();
341343

342-
List<JobExecution> executions = getJdbcTemplate().query(getQuery(GET_LAST_EXECUTION),
343-
new JobExecutionRowMapper(jobInstance), id, id);
344-
345-
Assert.state(executions.size() <= 1, "There must be at most one latest job execution");
346-
347-
if (executions.isEmpty()) {
348-
return null;
349-
}
350-
else {
351-
return executions.get(0);
344+
try (Stream<JobExecution> stream = getJdbcTemplate().queryForStream(getQuery(GET_LAST_EXECUTION),
345+
new JobExecutionRowMapper(jobInstance), id, id)) {
346+
return stream.findFirst().orElse(null);
352347
}
353348
}
354349

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.sql.Types;
2222
import java.util.ArrayList;
2323
import java.util.List;
24+
import java.util.stream.Stream;
2425

2526
import org.springframework.batch.core.DefaultJobKeyGenerator;
2627
import org.springframework.batch.core.JobExecution;
@@ -53,6 +54,7 @@
5354
* @author Will Schipp
5455
* @author Mahmoud Ben Hassine
5556
* @author Parikshit Dutta
57+
* @author Yanming Zhou
5658
*/
5759
public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements JobInstanceDao, InitializingBean {
5860

@@ -178,21 +180,12 @@ public JobInstance getJobInstance(final String jobName, final JobParameters jobP
178180

179181
RowMapper<JobInstance> rowMapper = new JobInstanceRowMapper();
180182

181-
List<JobInstance> instances;
182-
if (StringUtils.hasLength(jobKey)) {
183-
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_KEY), rowMapper, jobName, jobKey);
184-
}
185-
else {
186-
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_EMPTY_KEY), rowMapper, jobName, jobKey);
183+
try (Stream<JobInstance> stream = getJdbcTemplate().queryForStream(
184+
getQuery(StringUtils.hasLength(jobKey) ? FIND_JOBS_WITH_KEY : FIND_JOBS_WITH_EMPTY_KEY), rowMapper,
185+
jobName, jobKey)) {
186+
return stream.findFirst().orElse(null);
187187
}
188188

189-
if (instances.isEmpty()) {
190-
return null;
191-
}
192-
else {
193-
Assert.state(instances.size() == 1, "instance count must be 1 but was " + instances.size());
194-
return instances.get(0);
195-
}
196189
}
197190

198191
@Override

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.concurrent.locks.Lock;
3131
import java.util.concurrent.locks.ReentrantLock;
32+
import java.util.stream.Stream;
3233

3334
import org.apache.commons.logging.Log;
3435
import org.apache.commons.logging.LogFactory;
@@ -66,6 +67,7 @@
6667
* @author Mahmoud Ben Hassine
6768
* @author Baris Cubukcuoglu
6869
* @author Minsoo Kim
70+
* @author Yanming Zhou
6971
* @see StepExecutionDao
7072
*/
7173
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -324,16 +326,9 @@ private String truncateExitDescription(String description) {
324326
@Override
325327
@Nullable
326328
public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecutionId) {
327-
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION),
328-
new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId);
329-
330-
Assert.state(executions.size() <= 1,
331-
"There can be at most one step execution with given name for single job execution");
332-
if (executions.isEmpty()) {
333-
return null;
334-
}
335-
else {
336-
return executions.get(0);
329+
try (Stream<StepExecution> stream = getJdbcTemplate().queryForStream(getQuery(GET_STEP_EXECUTION),
330+
new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId)) {
331+
return stream.findFirst().orElse(null);
337332
}
338333
}
339334

0 commit comments

Comments
 (0)