OPEN - issue BATCH-1437: Support for CallbackPreferringPlatformTransactionManager (and for native TX in WAS)
Refactored TaskletStep so that it uses a TransactionCallback. It's actually much nicer that way because commit failures can be detected and accounted for with a proper rollback, instead of just panicking and using BatchStatus.UNKNOWN.
This commit is contained in:
@@ -194,11 +194,14 @@ public class TaskletStepExceptionTests {
|
||||
@Test
|
||||
public void testCommitError() throws Exception {
|
||||
|
||||
final RuntimeException exception = new RuntimeException();
|
||||
taskletStep.setTransactionManager(new ResourcelessTransactionManager() {
|
||||
@Override
|
||||
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
|
||||
throw exception;
|
||||
throw new RuntimeException("bar");
|
||||
}
|
||||
@Override
|
||||
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
|
||||
throw new RuntimeException("foo");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -213,7 +216,7 @@ public class TaskletStepExceptionTests {
|
||||
taskletStep.execute(stepExecution);
|
||||
assertEquals(UNKNOWN, stepExecution.getStatus());
|
||||
Throwable e = stepExecution.getFailureExceptions().get(0);
|
||||
assertEquals(exception, e.getCause());
|
||||
assertEquals("foo", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -17,7 +17,6 @@ package org.springframework.batch.core.step.tasklet;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
@@ -115,19 +114,18 @@ public class ChunkOrientedStepIntegrationTests {
|
||||
|
||||
jobRepository.add(stepExecution);
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus());
|
||||
// Exception on commit is not necessarily fatal: it should fail and rollback
|
||||
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
|
||||
StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobExecution.getJobInstance(), step
|
||||
.getName());
|
||||
assertEquals(lastStepExecution, stepExecution);
|
||||
assertFalse(lastStepExecution == stepExecution);
|
||||
|
||||
// If the StepExecution is not saved after the failure it will be
|
||||
// STARTED instead of UNKNOWN
|
||||
assertEquals(BatchStatus.UNKNOWN, lastStepExecution.getStatus());
|
||||
String msg = stepExecution.getExitStatus().getExitDescription();
|
||||
assertTrue(msg.contains("Fatal failure detected"));
|
||||
// STARTED instead of FAILED
|
||||
assertEquals(BatchStatus.FAILED, lastStepExecution.getStatus());
|
||||
// The original rollback was caused by this one:
|
||||
assertEquals("Simulate commit failure", stepExecution.getFailureExceptions().get(0).getCause().getMessage());
|
||||
assertEquals("Simulate commit failure", stepExecution.getFailureExceptions().get(0).getMessage());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -673,7 +673,7 @@ public class TaskletStepTests {
|
||||
String msg = stepExecution.getExitStatus().getExitDescription();
|
||||
assertTrue("Message does not contain ResetFailedException: " + msg, msg.contains("ResetFailedException"));
|
||||
// The original rollback was caused by this one:
|
||||
assertEquals("Bar", stepExecution.getFailureExceptions().get(0).getCause().getMessage());
|
||||
assertEquals("Bar", stepExecution.getFailureExceptions().get(0).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -682,6 +682,10 @@ public class TaskletStepTests {
|
||||
step.setTransactionManager(new ResourcelessTransactionManager() {
|
||||
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
|
||||
// Simulate failure on commit
|
||||
throw new RuntimeException("Foo");
|
||||
}
|
||||
@Override
|
||||
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
|
||||
throw new RuntimeException("Bar");
|
||||
}
|
||||
});
|
||||
@@ -695,12 +699,10 @@ public class TaskletStepTests {
|
||||
step.execute(stepExecution);
|
||||
assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus());
|
||||
String msg = stepExecution.getExitStatus().getExitDescription();
|
||||
assertTrue(msg.contains("Fatal failure detected"));
|
||||
Throwable ex = stepExecution.getFailureExceptions().get(0);
|
||||
msg = ex.getMessage();
|
||||
assertTrue(msg.contains("Fatal failure detected"));
|
||||
// The original rollback was caused by this one:
|
||||
assertEquals("Bar", ex.getCause().getMessage());
|
||||
// The original rollback failed because of this one:
|
||||
assertEquals("Bar", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user