Skip to content

Commit e329b61

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 4a4c822 commit e329b61

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
* @author Mahmoud Ben Hassine
7272
* @author Dimitrios Liapis
7373
* @author Philippe Marschall
74+
* @author Jinwoo Bae
7475
*/
7576
public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean {
7677

@@ -492,8 +493,6 @@ private final class JobExecutionRowMapper implements RowMapper<JobExecution> {
492493

493494
private JobInstance jobInstance;
494495

495-
private JobParameters jobParameters;
496-
497496
public JobExecutionRowMapper() {
498497
}
499498

@@ -505,9 +504,7 @@ public JobExecutionRowMapper(JobInstance jobInstance) {
505504
public JobExecution mapRow(ResultSet rs, int rowNum) throws SQLException {
506505
Long id = rs.getLong(1);
507506
JobExecution jobExecution;
508-
if (jobParameters == null) {
509-
jobParameters = getJobParameters(id);
510-
}
507+
JobParameters jobParameters = getJobParameters(id);
511508

512509
if (jobInstance == null) {
513510
jobExecution = new JobExecution(id, jobParameters);

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

Lines changed: 40 additions & 1 deletion
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.
@@ -40,6 +40,7 @@
4040
import org.springframework.batch.core.JobInstance;
4141
import org.springframework.batch.core.JobInterruptedException;
4242
import org.springframework.batch.core.JobParameters;
43+
import org.springframework.batch.core.JobParametersBuilder;
4344
import org.springframework.batch.core.Step;
4445
import org.springframework.batch.core.StepExecution;
4546
import org.springframework.batch.core.UnexpectedJobExecutionException;
@@ -68,6 +69,7 @@
6869
* @author Lucas Ward
6970
* @author Will Schipp
7071
* @author Mahmoud Ben Hassine
72+
* @author Jinwoo Bae
7173
*/
7274
class SimpleJobTests {
7375

@@ -483,6 +485,43 @@ void testGetStepNotExists() {
483485
assertNull(step);
484486
}
485487

488+
@Test
489+
void testGetMultipleJobParameters() throws Exception {
490+
StubStep failStep = new StubStep("failStep", jobRepository);
491+
492+
failStep.setCallback(new Runnable() {
493+
@Override
494+
public void run() {
495+
throw new RuntimeException("An error occurred.");
496+
}
497+
});
498+
499+
job.setName("parametersTestJob");
500+
job.setSteps(Arrays.asList(new Step[] { failStep }));
501+
502+
JobParameters firstJobParameters = new JobParametersBuilder().addString("JobExecutionParameter", "first", false)
503+
.toJobParameters();
504+
JobExecution jobexecution = jobRepository.createJobExecution(job.getName(), firstJobParameters);
505+
job.execute(jobexecution);
506+
507+
List<JobExecution> jobExecutionList = jobExplorer.getJobExecutions(jobexecution.getJobInstance());
508+
509+
assertEquals(jobExecutionList.size(), 1);
510+
assertEquals(jobExecutionList.get(0).getJobParameters().getString("JobExecutionParameter"), "first");
511+
512+
JobParameters secondJobParameters = new JobParametersBuilder()
513+
.addString("JobExecutionParameter", "second", false).toJobParameters();
514+
jobexecution = jobRepository.createJobExecution(job.getName(), secondJobParameters);
515+
job.execute(jobexecution);
516+
517+
jobExecutionList = jobExplorer.getJobExecutions(jobexecution.getJobInstance());
518+
519+
assertEquals(jobExecutionList.size(), 2);
520+
assertEquals(jobExecutionList.get(0).getJobParameters().getString("JobExecutionParameter"), "second");
521+
assertEquals(jobExecutionList.get(1).getJobParameters().getString("JobExecutionParameter"), "first");
522+
523+
}
524+
486525
/*
487526
* Check JobRepository to ensure status is being saved.
488527
*/

0 commit comments

Comments
 (0)