Skip to content

Commit 957feb4

Browse files
committed
Use jdbcTemplate.queryForStream().findFirst()
Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 08c4cb1 commit 957feb4

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/jdbc/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;
@@ -58,6 +58,7 @@
5858
* @author Michael Minella
5959
* @author David Turanski
6060
* @author Mahmoud Ben Hassine
61+
* @author Yanming Zhou
6162
*/
6263
public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao {
6364

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

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

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

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

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/jdbc/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;
@@ -77,6 +78,7 @@
7778
* @author Dimitrios Liapis
7879
* @author Philippe Marschall
7980
* @author Jinwoo Bae
81+
* @author Yanming Zhou
8082
*/
8183
public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean {
8284

@@ -342,16 +344,9 @@ public JobExecution getLastJobExecution(JobInstance jobInstance) {
342344

343345
Long id = jobInstance.getId();
344346

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

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/jdbc/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;
@@ -55,6 +56,7 @@
5556
* @author Will Schipp
5657
* @author Mahmoud Ben Hassine
5758
* @author Parikshit Dutta
59+
* @author Yanming Zhou
5860
*/
5961
public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements JobInstanceDao, InitializingBean {
6062

@@ -174,21 +176,12 @@ public JobInstance getJobInstance(final String jobName, final JobParameters jobP
174176

175177
RowMapper<JobInstance> rowMapper = new JobInstanceRowMapper();
176178

177-
List<JobInstance> instances;
178-
if (StringUtils.hasLength(jobKey)) {
179-
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_KEY), rowMapper, jobName, jobKey);
180-
}
181-
else {
182-
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_EMPTY_KEY), rowMapper, jobName, jobKey);
179+
try (Stream<JobInstance> stream = getJdbcTemplate().queryForStream(
180+
getQuery(StringUtils.hasLength(jobKey) ? FIND_JOBS_WITH_KEY : FIND_JOBS_WITH_EMPTY_KEY), rowMapper,
181+
jobName, jobKey)) {
182+
return stream.findFirst().orElse(null);
183183
}
184184

185-
if (instances.isEmpty()) {
186-
return null;
187-
}
188-
else {
189-
Assert.state(instances.size() == 1, "instance count must be 1 but was " + instances.size());
190-
return instances.get(0);
191-
}
192185
}
193186

194187
@Override

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/jdbc/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;
@@ -68,6 +69,7 @@
6869
* @author Mahmoud Ben Hassine
6970
* @author Baris Cubukcuoglu
7071
* @author Minsoo Kim
72+
* @author Yanming Zhou
7173
* @see StepExecutionDao
7274
*/
7375
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -326,16 +328,9 @@ private String truncateExitDescription(String description) {
326328
@Override
327329
@Nullable
328330
public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecutionId) {
329-
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION),
330-
new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId);
331-
332-
Assert.state(executions.size() <= 1,
333-
"There can be at most one step execution with given name for single job execution");
334-
if (executions.isEmpty()) {
335-
return null;
336-
}
337-
else {
338-
return executions.get(0);
331+
try (Stream<StepExecution> stream = getJdbcTemplate().queryForStream(getQuery(GET_STEP_EXECUTION),
332+
new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId)) {
333+
return stream.findFirst().orElse(null);
339334
}
340335
}
341336

0 commit comments

Comments
 (0)