Skip to content

Use jdbcTemplate.queryForStream().findFirst() #4802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -27,11 +27,11 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

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

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

List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_JOB_EXECUTION_CONTEXT),
new ExecutionContextRowMapper(), executionId);
if (!results.isEmpty()) {
return results.get(0);
}
else {
return new ExecutionContext();
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_JOB_EXECUTION_CONTEXT),
new ExecutionContextRowMapper(), executionId)) {
return stream.findFirst().orElseGet(ExecutionContext::new);
}
}

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

List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_STEP_EXECUTION_CONTEXT),
new ExecutionContextRowMapper(), executionId);
if (results.size() > 0) {
return results.get(0);
}
else {
return new ExecutionContext();
try (Stream<ExecutionContext> stream = getJdbcTemplate().queryForStream(getQuery(FIND_STEP_EXECUTION_CONTEXT),
new ExecutionContextRowMapper(), executionId)) {
return stream.findFirst().orElseGet(ExecutionContext::new);
}
}

Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

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

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

Long id = jobInstance.getId();

List<JobExecution> executions = getJdbcTemplate().query(getQuery(GET_LAST_EXECUTION),
new JobExecutionRowMapper(jobInstance), id, id);

Assert.state(executions.size() <= 1, "There must be at most one latest job execution");

if (executions.isEmpty()) {
return null;
}
else {
return executions.get(0);
try (Stream<JobExecution> stream = getJdbcTemplate().queryForStream(getQuery(GET_LAST_EXECUTION),
new JobExecutionRowMapper(jobInstance), id, id)) {
return stream.findFirst().orElse(null);
}
}

Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

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

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

RowMapper<JobInstance> rowMapper = new JobInstanceRowMapper();

List<JobInstance> instances;
if (StringUtils.hasLength(jobKey)) {
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_KEY), rowMapper, jobName, jobKey);
}
else {
instances = getJdbcTemplate().query(getQuery(FIND_JOBS_WITH_EMPTY_KEY), rowMapper, jobName, jobKey);
try (Stream<JobInstance> stream = getJdbcTemplate().queryForStream(
getQuery(StringUtils.hasLength(jobKey) ? FIND_JOBS_WITH_KEY : FIND_JOBS_WITH_EMPTY_KEY), rowMapper,
jobName, jobKey)) {
return stream.findFirst().orElse(null);
}

if (instances.isEmpty()) {
return null;
}
else {
Assert.state(instances.size() == 1, "instance count must be 1 but was " + instances.size());
return instances.get(0);
}
}

@Override
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -66,6 +67,7 @@
* @author Mahmoud Ben Hassine
* @author Baris Cubukcuoglu
* @author Minsoo Kim
* @author Yanming Zhou
* @see StepExecutionDao
*/
public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implements StepExecutionDao, InitializingBean {
@@ -324,16 +326,9 @@ private String truncateExitDescription(String description) {
@Override
@Nullable
public StepExecution getStepExecution(JobExecution jobExecution, Long stepExecutionId) {
List<StepExecution> executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION),
new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId);

Assert.state(executions.size() <= 1,
"There can be at most one step execution with given name for single job execution");
if (executions.isEmpty()) {
return null;
}
else {
return executions.get(0);
try (Stream<StepExecution> stream = getJdbcTemplate().queryForStream(getQuery(GET_STEP_EXECUTION),
new StepExecutionRowMapper(jobExecution), jobExecution.getId(), stepExecutionId)) {
return stream.findFirst().orElse(null);
}
}