Skip to content

Commit df1e4a4

Browse files
author
robokaso
committed
IN PROGRESS - BATCH-929: Deferrable Constraints cause unrecoverable errors
count non-fatal commit failure as rollback
1 parent b094db6 commit df1e4a4

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ public RepeatStatus doInStepContext(RepeatContext repeatContext, StepContext ste
297297
}
298298
catch (Exception e) {
299299
if (nonFatalCommitExceptions.classify(e)) {
300-
stepExecution.setExecutionContext(getJobRepository().getExecutionContext(stepExecution));
301-
JobExecution jobExecution = stepExecution.getJobExecution();
302-
jobExecution.setExecutionContext(getJobRepository().getExecutionContext(jobExecution));
300+
rollbackExecutionContext(stepExecution);
303301
throw new CommitException("non-fatal commit failure", e);
304302
}
305303
else {
@@ -326,10 +324,15 @@ public RepeatStatus doInStepContext(RepeatContext repeatContext, StepContext ste
326324
throw e;
327325
}
328326
catch (Exception e) {
329-
// if commit failed, calling rollback on tx manager would cause exception
327+
// if commit failed, calling rollback on tx manager would
328+
// cause exception
330329
if (!(e instanceof CommitException)) {
331330
processRollback(stepExecution, fatalException, transaction);
332331
}
332+
else {
333+
// assume the failed commit caused rollback
334+
stepExecution.rollback();
335+
}
333336
throw e;
334337
}
335338
finally {
@@ -348,6 +351,15 @@ public RepeatStatus doInStepContext(RepeatContext repeatContext, StepContext ste
348351
return result;
349352
}
350353

354+
/**
355+
* Load the saved value of ExecutionContext from repository.
356+
*/
357+
private void rollbackExecutionContext(StepExecution stepExecution) {
358+
stepExecution.setExecutionContext(getJobRepository().getExecutionContext(stepExecution));
359+
JobExecution jobExecution = stepExecution.getJobExecution();
360+
jobExecution.setExecutionContext(getJobRepository().getExecutionContext(jobExecution));
361+
}
362+
351363
});
352364

353365
}

spring-batch-core/src/test/java/org/springframework/batch/core/step/item/TaskletStepExceptionTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public RepeatStatus execute(StepContribution contribution, AttributeAccessor att
199199
* status and execution context should be rolled back.
200200
*/
201201
@Test
202-
public void testSkippableCommitError() throws Exception {
202+
public void testNonFatalCommitError() throws Exception {
203203

204204
class TestItemStream extends ItemStreamSupport {
205205
private boolean called = false;
@@ -240,6 +240,7 @@ public RepeatStatus execute(StepContribution contribution, AttributeAccessor att
240240
assertEquals("step won't refuse to restart", FAILED, stepExecution.getStatus());
241241
assertTrue("execution context modified", stream.called);
242242
assertTrue("execution context rolled back", stepExecution.getExecutionContext().isEmpty());
243+
assertEquals("failed commit counted as rollback", 1, stepExecution.getRollbackCount());
243244
}
244245

245246
@Test

spring-batch-integration/src/test/java/org/springframework/batch/integration/JobRepositorySupport.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
2424
import org.springframework.batch.core.repository.JobRepository;
2525
import org.springframework.batch.core.repository.JobRestartException;
26+
import org.springframework.batch.item.ExecutionContext;
2627

2728
/**
2829
* @author Dave Syer
@@ -87,4 +88,12 @@ public JobExecution getLastJobExecution(String jobName, JobParameters jobParamet
8788
return null;
8889
}
8990

91+
public ExecutionContext getExecutionContext(StepExecution stepExecution) {
92+
return null;
93+
}
94+
95+
public ExecutionContext getExecutionContext(JobExecution jobExecution) {
96+
return null;
97+
}
98+
9099
}

0 commit comments

Comments
 (0)