Skip to content

Commit 8018705

Browse files
committed
Revert "Use jdbcTemplate.queryForStream().findFirst() where appropriate"
This reverts commit b88e7d7 Resolves #5006
1 parent d90a02d commit 8018705

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

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

Lines changed: 15 additions & 8 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;
3031
import java.util.Map;
3132
import java.util.Map.Entry;
3233
import java.util.concurrent.locks.Lock;
3334
import java.util.concurrent.locks.ReentrantLock;
34-
import java.util.stream.Stream;
3535

3636
import org.springframework.batch.core.job.JobExecution;
3737

@@ -58,7 +58,6 @@
5858
* @author Michael Minella
5959
* @author David Turanski
6060
* @author Mahmoud Ben Hassine
61-
* @author Yanming Zhou
6261
*/
6362
public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao {
6463

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

157-
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_JOB_EXECUTION_CONTEXT),
158-
new ExecutionContextRowMapper(), executionId)) {
159-
return stream.findFirst().orElseGet(ExecutionContext::new);
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();
160163
}
161164
}
162165

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

168-
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_STEP_EXECUTION_CONTEXT),
169-
new ExecutionContextRowMapper(), executionId)) {
170-
return stream.findFirst().orElseGet(ExecutionContext::new);
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();
171178
}
172179
}
173180

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

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

2625
import org.springframework.batch.core.job.DefaultJobKeyGenerator;
2726
import org.springframework.batch.core.job.JobExecution;
@@ -186,12 +185,21 @@ public JobInstance getJobInstance(final String jobName, final JobParameters jobP
186185

187186
RowMapper<JobInstance> rowMapper = new JobInstanceRowMapper();
188187

189-
try (Stream<JobInstance> stream = getJdbcTemplate().queryForStream(
190-
getQuery(StringUtils.hasLength(jobKey) ? FIND_JOBS_WITH_KEY : FIND_JOBS_WITH_EMPTY_KEY), rowMapper,
191-
jobName, jobKey)) {
192-
return stream.findFirst().orElse(null);
188+
List<JobInstance> instances;
189+
if (StringUtils.hasLength(jobKey)) {
190+
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_KEY), rowMapper, jobName, jobKey);
191+
}
192+
else {
193+
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_EMPTY_KEY), rowMapper, jobName, jobKey);
193194
}
194195

196+
if (instances.isEmpty()) {
197+
return null;
198+
}
199+
else {
200+
Assert.state(instances.size() == 1, "instance count must be 1 but was " + instances.size());
201+
return instances.get(0);
202+
}
195203
}
196204

197205
@Override

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

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

3332
import org.apache.commons.logging.Log;
3433
import org.apache.commons.logging.LogFactory;
@@ -306,9 +305,16 @@ private long getJobExecutionId(long stepExecutionId) {
306305
@Nullable
307306
@Deprecated(since = "6.0", forRemoval = true)
308307
public StepExecution getStepExecution(JobExecution jobExecution, long stepExecutionId) {
309-
try (Stream<StepExecution> stream = getJdbcTemplate().queryForStream(getQuery(GET_STEP_EXECUTION),
310-
new StepExecutionRowMapper(jobExecution), stepExecutionId)) {
311-
return stream.findFirst().orElse(null);
308+
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION),
309+
new StepExecutionRowMapper(jobExecution), stepExecutionId);
310+
311+
Assert.state(executions.size() <= 1,
312+
"There can be at most one step execution with given name for single job execution");
313+
if (executions.isEmpty()) {
314+
return null;
315+
}
316+
else {
317+
return executions.get(0);
312318
}
313319
}
314320

0 commit comments

Comments
 (0)