Skip to content

Commit 0103ef0

Browse files
committed
Fix object graph deletion in SimpleJobRepository#deleteJobInstance
Before this commit, SimpleJobRepository#deleteJobInstance was only deleting the given job instance and not the entire object graph, which leads to data inconsistency (orphan job executions and invalid foreign keys). This commit fixes the contract and the implementation of the method to delete the entire object graph. Resolves #4250
1 parent 4629294 commit 0103ef0

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

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

Lines changed: 4 additions & 2 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.
@@ -244,7 +244,9 @@ default void deleteJobExecution(JobExecution jobExecution) {
244244
}
245245

246246
/**
247-
* Delete the job instance.
247+
* Delete the job instance object graph (ie the job instance with all associated job
248+
* executions along with their respective object graphs as specified in
249+
* {@link #deleteJobExecution(JobExecution)}).
248250
* @param jobInstance the job instance to delete
249251
* @since 5.0
250252
*/

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

Lines changed: 3 additions & 2 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.
@@ -131,7 +131,8 @@ default JobInstance getLastJobInstance(String jobName) {
131131
long getJobInstanceCount(@Nullable String jobName) throws NoSuchJobException;
132132

133133
/**
134-
* Delete the job instance.
134+
* Delete the job instance. This method is not expected to delete the associated job
135+
* executions. If this is needed, clients of this method should do that manually.
135136
* @param jobInstance the job instance to delete
136137
* @since 5.0
137138
*/

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ public void deleteJobExecution(JobExecution jobExecution) {
328328

329329
@Override
330330
public void deleteJobInstance(JobInstance jobInstance) {
331+
List<JobExecution> jobExecutions = this.jobExecutionDao.findJobExecutions(jobInstance);
332+
for (JobExecution jobExecution : jobExecutions) {
333+
deleteJobExecution(jobExecution);
334+
}
331335
this.jobInstanceDao.deleteJobInstance(jobInstance);
332336
}
333337

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,22 @@ void testDeleteJobExecution() {
366366
verify(this.jobExecutionDao).deleteJobExecution(jobExecution);
367367
}
368368

369+
@Test
370+
void testDeleteJobInstance() {
371+
// given
372+
JobExecution jobExecution1 = mock(JobExecution.class);
373+
JobExecution jobExecution2 = mock(JobExecution.class);
374+
JobInstance jobInstance = mock(JobInstance.class);
375+
when(this.jobExecutionDao.findJobExecutions(jobInstance))
376+
.thenReturn(Arrays.asList(jobExecution1, jobExecution2));
377+
378+
// when
379+
this.jobRepository.deleteJobInstance(jobInstance);
380+
381+
// then
382+
verify(this.jobExecutionDao).deleteJobExecution(jobExecution1);
383+
verify(this.jobExecutionDao).deleteJobExecution(jobExecution2);
384+
verify(this.jobInstanceDao).deleteJobInstance(jobInstance);
385+
}
386+
369387
}

0 commit comments

Comments
 (0)