Skip to content

Commit 539ec71

Browse files
baezzysfmbenhassine
authored andcommitted
Fix retrieval of job parameters in SimpleJobExplorer#getJobExecutions
Before this commit, SimpleJobExplorer#getJobExecutions returned job executions with wrong job parameters, ie a job execution could have the parameter of another execution. This commit fixes the implementation so that each returned job execution has its own parameters. Resolves #4246
1 parent 9874a65 commit 539ec71

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,7 @@
6161
* @author Michael Minella
6262
* @author Mahmoud Ben Hassine
6363
* @author Dimitrios Liapis
64+
* @author Jinwoo Bae
6465
*/
6566
public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean {
6667

@@ -403,8 +404,6 @@ private final class JobExecutionRowMapper implements RowMapper<JobExecution> {
403404

404405
private JobInstance jobInstance;
405406

406-
private JobParameters jobParameters;
407-
408407
public JobExecutionRowMapper() {
409408
}
410409

@@ -417,9 +416,7 @@ public JobExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
417416
Long id = rs.getLong(1);
418417
String jobConfigurationLocation = rs.getString(10);
419418
JobExecution jobExecution;
420-
if (jobParameters == null) {
421-
jobParameters = getJobParameters(id);
422-
}
419+
JobParameters jobParameters = getJobParameters(id);
423420

424421
if (jobInstance == null) {
425422
jobExecution = new JobExecution(id, jobParameters, jobConfigurationLocation);

spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,8 @@
4545
import org.springframework.batch.core.Step;
4646
import org.springframework.batch.core.StepExecution;
4747
import org.springframework.batch.core.UnexpectedJobExecutionException;
48+
import org.springframework.batch.core.explore.JobExplorer;
49+
import org.springframework.batch.core.explore.support.SimpleJobExplorer;
4850
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
4951
import org.springframework.batch.core.repository.JobRepository;
5052
import org.springframework.batch.core.repository.dao.ExecutionContextDao;
@@ -66,11 +68,14 @@
6668
* @author Lucas Ward
6769
* @author Will Schipp
6870
* @author Mahmoud Ben Hassine
71+
* @author Jinwoo Bae
6972
*/
7073
public class SimpleJobTests {
7174

7275
private JobRepository jobRepository;
7376

77+
private JobExplorer jobExplorer;
78+
7479
private JobInstanceDao jobInstanceDao;
7580

7681
private JobExecutionDao jobExecutionDao;
@@ -105,9 +110,11 @@ public void setUp() throws Exception {
105110
stepExecutionDao = new MapStepExecutionDao();
106111
ecDao = new MapExecutionContextDao();
107112
jobRepository = new SimpleJobRepository(jobInstanceDao, jobExecutionDao, stepExecutionDao, ecDao);
113+
jobExplorer = new SimpleJobExplorer(jobInstanceDao, jobExecutionDao, stepExecutionDao, ecDao);
108114
job = new SimpleJob();
109115
job.setJobRepository(jobRepository);
110116

117+
111118
step1 = new StubStep("TestStep1", jobRepository);
112119
step1.setCallback(new Runnable() {
113120
@Override
@@ -524,6 +531,43 @@ public void testGetStepNotExists() {
524531
assertNull(step);
525532
}
526533

534+
@Test
535+
public void testGetMultipleJobParameters() throws Exception {
536+
StubStep failStep = new StubStep("failStep", jobRepository);
537+
538+
failStep.setCallback(new Runnable() {
539+
@Override
540+
public void run() {
541+
throw new RuntimeException("An error occurred.");
542+
}
543+
});
544+
545+
job.setName("parametersTestJob");
546+
job.setSteps(Arrays.asList(new Step[] { failStep }));
547+
548+
JobParameters firstJobParameters = new JobParametersBuilder().addString("JobExecutionParameter", "first", false)
549+
.toJobParameters();
550+
JobExecution jobexecution = jobRepository.createJobExecution(job.getName(), firstJobParameters);
551+
job.execute(jobexecution);
552+
553+
List<JobExecution> jobExecutionList = jobExplorer.getJobExecutions(jobexecution.getJobInstance());
554+
555+
assertEquals(jobExecutionList.size(), 1);
556+
assertEquals(jobExecutionList.get(0).getJobParameters().getString("JobExecutionParameter"), "first");
557+
558+
JobParameters secondJobParameters = new JobParametersBuilder()
559+
.addString("JobExecutionParameter", "second", false).toJobParameters();
560+
jobexecution = jobRepository.createJobExecution(job.getName(), secondJobParameters);
561+
job.execute(jobexecution);
562+
563+
jobExecutionList = jobExplorer.getJobExecutions(jobexecution.getJobInstance());
564+
565+
assertEquals(jobExecutionList.size(), 2);
566+
assertEquals(jobExecutionList.get(0).getJobParameters().getString("JobExecutionParameter"), "second");
567+
assertEquals(jobExecutionList.get(1).getJobParameters().getString("JobExecutionParameter"), "first");
568+
569+
}
570+
527571
/*
528572
* Check JobRepository to ensure status is being saved.
529573
*/

0 commit comments

Comments
 (0)