REOPENED - BATCH-765: StepExecution should be saved on every commit

wrapped StepExecution update in try/catch and added testcase for failure scenario
This commit is contained in:
robokaso
2008-08-13 09:26:07 +00:00
parent 55d59ff09e
commit b9810c6eeb
2 changed files with 53 additions and 3 deletions

View File

@@ -35,6 +35,7 @@ import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.dao.MapExecutionContextDao;
import org.springframework.batch.core.repository.dao.MapJobExecutionDao;
import org.springframework.batch.core.repository.dao.MapJobInstanceDao;
@@ -59,6 +60,7 @@ import org.springframework.batch.repeat.policy.DefaultResultCompletionPolicy;
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
import org.springframework.batch.repeat.support.RepeatTemplate;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.DefaultTransactionStatus;
@@ -135,6 +137,9 @@ public class StepHandlerStepTests extends TestCase {
assertEquals(1, stepExecution.getItemCount());
}
/**
* StepExecution should be updated after every chunk commit.
*/
public void testStepExecutionUpdates() throws Exception {
JobExecution jobExecution = new JobExecution(jobInstance);
@@ -152,6 +157,31 @@ public class StepHandlerStepTests extends TestCase {
assertTrue(3 <= jobRepository.updateCount);
}
/**
* Failure to update StepExecution after chunk commit is fatal.
*/
public void testStepExecutionUpdateFailure() throws Exception {
JobExecution jobExecution = new JobExecution(jobInstance);
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
JobRepository repository = new JobRepositoryFailedUpdateStub();
step.setJobRepository(repository);
step.afterPropertiesSet();
try {
step.execute(stepExecution);
fail();
}
catch (Exception e) {
assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus());
assertEquals("Fatal error detected during update of step execution", e.getMessage());
assertEquals("stub exception", e.getCause().getMessage());
}
}
public void testChunkExecutor() throws Exception {
RepeatTemplate template = new RepeatTemplate();
@@ -810,8 +840,21 @@ public class StepHandlerStepTests extends TestCase {
}
}
private static class JobRepositoryFailedUpdateStub extends JobRepositorySupport {
private boolean firstCall = true;
public void update(StepExecution stepExecution) {
if (firstCall) {
firstCall = false;
throw new DataAccessResourceFailureException("stub exception");
}
}
}
private class MockRestartableItemReader extends ItemStreamSupport implements ItemReader<String>, StepExecutionListener {
private class MockRestartableItemReader extends ItemStreamSupport implements ItemReader<String>,
StepExecutionListener {
private boolean getExecutionAttributesCalled = false;