Skip to content

Commit 323fc06

Browse files
committed
Replace queryForStream with query to prevent connection leaks
Signed-off-by: Hyunsang Han <[email protected]>
1 parent 21af80d commit 323fc06

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.Map.Entry;
3232
import java.util.concurrent.locks.Lock;
3333
import java.util.concurrent.locks.ReentrantLock;
34-
import java.util.stream.Stream;
34+
import java.util.List;
3535

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

@@ -59,6 +59,7 @@
5959
* @author David Turanski
6060
* @author Mahmoud Ben Hassine
6161
* @author Yanming Zhou
62+
* @author Hyunsang Han
6263
*/
6364
public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao {
6465

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

157-
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_JOB_EXECUTION_CONTEXT),
158-
new ExecutionContextRowMapper(), executionId)) {
159-
return stream.findFirst().orElseGet(ExecutionContext::new);
160-
}
158+
List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_JOB_EXECUTION_CONTEXT),
159+
new ExecutionContextRowMapper(), executionId);
160+
return !results.isEmpty() ? results.get(0) : new ExecutionContext();
161161
}
162162

163163
@Override
164164
public ExecutionContext getExecutionContext(StepExecution stepExecution) {
165165
Long executionId = stepExecution.getId();
166166
Assert.notNull(executionId, "ExecutionId must not be null.");
167167

168-
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_STEP_EXECUTION_CONTEXT),
169-
new ExecutionContextRowMapper(), executionId)) {
170-
return stream.findFirst().orElseGet(ExecutionContext::new);
171-
}
168+
List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_STEP_EXECUTION_CONTEXT),
169+
new ExecutionContextRowMapper(), executionId);
170+
return !results.isEmpty() ? results.get(0) : new ExecutionContext();
172171
}
173172

174173
@Override

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

Lines changed: 7 additions & 4 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;
@@ -58,6 +57,7 @@
5857
* @author Mahmoud Ben Hassine
5958
* @author Parikshit Dutta
6059
* @author Yanming Zhou
60+
* @author Hyunsang Han
6161
*/
6262
public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements JobInstanceDao, InitializingBean {
6363

@@ -186,11 +186,14 @@ public JobInstance getJobInstance(final String jobName, final JobParameters jobP
186186

187187
RowMapper<JobInstance> rowMapper = new JobInstanceRowMapper();
188188

189-
try (Stream<JobInstance> stream = getJdbcTemplate().queryForStream(
189+
List<JobInstance> instances = getJdbcTemplate().query(
190190
getQuery(StringUtils.hasLength(jobKey) ? FIND_JOBS_WITH_KEY : FIND_JOBS_WITH_EMPTY_KEY), rowMapper,
191-
jobName, jobKey)) {
192-
return stream.findFirst().orElse(null);
191+
jobName, jobKey);
192+
193+
if (!instances.isEmpty()) {
194+
Assert.state(instances.size() == 1, "instance count must be 1 but was " + instances.size());
193195
}
196+
return instances.isEmpty() ? null : instances.get(0);
194197

195198
}
196199

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

Lines changed: 7 additions & 5 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;
@@ -68,6 +67,7 @@
6867
* @author Baris Cubukcuoglu
6968
* @author Minsoo Kim
7069
* @author Yanming Zhou
70+
* @author Hyunsang Han
7171
* @see StepExecutionDao
7272
*/
7373
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -306,10 +306,12 @@ private long getJobExecutionId(long stepExecutionId) {
306306
@Nullable
307307
@Deprecated(since = "6.0", forRemoval = true)
308308
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);
312-
}
309+
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION),
310+
new StepExecutionRowMapper(jobExecution), stepExecutionId);
311+
312+
Assert.state(executions.size() <= 1,
313+
"There can be at most one step execution with given name for single job execution");
314+
return executions.isEmpty() ? null : executions.get(0);
313315
}
314316

315317
@Override

0 commit comments

Comments
 (0)