Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b88e7d7

Browse files
quafffmbenhassine
authored andcommitted
Use jdbcTemplate.queryForStream().findFirst() where appropriate
Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 92a304e commit b88e7d7

File tree

4 files changed

+21
-48
lines changed

4 files changed

+21
-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: 4 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;
@@ -336,16 +337,9 @@ public JobExecution getLastJobExecution(JobInstance jobInstance) {
336337

337338
Long id = jobInstance.getId();
338339

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

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

Lines changed: 5 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;
@@ -173,21 +174,12 @@ public JobInstance getJobInstance(final String jobName, final JobParameters jobP
173174

174175
RowMapper<JobInstance> rowMapper = new JobInstanceRowMapper();
175176

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

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

193185
@Override

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

Lines changed: 4 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;
@@ -326,16 +327,9 @@ private String truncateExitDescription(String description) {
326327
@Override
327328
@Nullable
328329
public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecutionId) {
329-
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION),
330-
new StepExecutionRowMapper(jobExecution), 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);
330+
try (Stream<StepExecution> stream = getJdbcTemplate().queryForStream(getQuery(GET_STEP_EXECUTION),
331+
new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId)) {
332+
return stream.findFirst().orElse(null);
339333
}
340334
}
341335

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /