From 120c9cc512f02df93b8f7ef56ae46fd040f5223d Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 13 Nov 2007 15:49:51 +0000 Subject: [PATCH] RESOLVED - issue BATCH-199: ExceptionHandler in SImpleStepConfiguration ignored by SimpleStepConfigurationFactory http://opensource.atlassian.com/projects/spring/browse/BATCH-199 --- .../PrototypeBeanStepExecutorFactory.java | 1 + .../step/simple/SimpleStepExecutor.java | 82 ++++++++++--------- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/execution/src/main/java/org/springframework/batch/execution/step/PrototypeBeanStepExecutorFactory.java b/execution/src/main/java/org/springframework/batch/execution/step/PrototypeBeanStepExecutorFactory.java index 8f7c47de5..f7c8ccb94 100644 --- a/execution/src/main/java/org/springframework/batch/execution/step/PrototypeBeanStepExecutorFactory.java +++ b/execution/src/main/java/org/springframework/batch/execution/step/PrototypeBeanStepExecutorFactory.java @@ -123,6 +123,7 @@ public class PrototypeBeanStepExecutorFactory implements StepExecutorFactory, template.setCompletionPolicy(new SimpleCompletionPolicy( ((SimpleStepConfiguration) configuration) .getCommitInterval())); + template.setExceptionHandler(((SimpleStepConfiguration)configuration).getExceptionHandler()); } ((SimpleStepExecutor) executor) .setChunkOperations(repeatOperations); diff --git a/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java b/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java index 40d71427f..7fc00c4dd 100644 --- a/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java +++ b/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java @@ -164,7 +164,7 @@ public class SimpleStepExecutor implements StepExecutor { // Add the job identifier so that it can be used to identify // the conversation in StepScope stepScopeContext.setAttribute(StepScope.ID_KEY, stepExecution - .getJobExecution().getJob().getIdentifier()); + .getJobExecution().getJob().getIdentifier()); try { stepExecution @@ -198,47 +198,51 @@ public class SimpleStepExecutor implements StepExecutor { // interruption. interruptionPolicy.checkInterrupted(context); - ExitStatus result = (ExitStatus) new TransactionTemplate( - transactionManager) - .execute(new TransactionCallback() { - public Object doInTransaction( - TransactionStatus status) { - // New transaction obtained, resynchronize - // TransactionSyncrhonization objects - BatchTransactionSynchronizationManager - .resynchronize(); - ExitStatus result; + ExitStatus result; + try { + result = (ExitStatus) new TransactionTemplate( + transactionManager) + .execute(new TransactionCallback() { + public Object doInTransaction( + TransactionStatus status) { + // New transaction obtained, + // resynchronize + // TransactionSyncrhonization objects + BatchTransactionSynchronizationManager + .resynchronize(); + ExitStatus result; - try { result = processChunk(configuration, stepExecution); - } catch (Throwable t) { - /* - * any exception thrown within the - * transaction template will - * automatically cause the transaction - * to rollback - */ - stepExecution.incrementRollbackCount(); - if (t instanceof RuntimeException) { - throw (RuntimeException) t; - } else { - throw new RuntimeException(t); - } - } - if (saveRestartData) { - step - .setRestartData(getRestartData(module)); - jobRepository.update(step); + if (saveRestartData) { + step + .setRestartData(getRestartData(module)); + jobRepository.update(step); + } + Properties statistics = getStatistics(module); + stepExecution.setStatistics(statistics); + stepExecution.incrementCommitCount(); + jobRepository + .saveOrUpdate(stepExecution); + return result; } - Properties statistics = getStatistics(module); - stepExecution.setStatistics(statistics); - stepExecution.incrementCommitCount(); - jobRepository.saveOrUpdate(stepExecution); - return result; - } - }); + }); + } catch (Throwable t) { + /* + * Any exception thrown within the transaction template + * will automatically cause the transaction to rollback. + * We need to include exceptions during an attempted + * commit (e.g. Hibernate flush) so this catch block + * comes outside the transaction. + */ + stepExecution.incrementRollbackCount(); + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } else { + throw new RuntimeException(t); + } + } // Check for interruption after transaction as well, so that // the interrupted exception is correctly propagated up to @@ -358,8 +362,8 @@ public class SimpleStepExecutor implements StepExecutor { * @throws Exception * if there is an error */ - protected ExitStatus doTaskletProcessing(Tasklet tasklet, StepExecution stepExecution) - throws Exception { + protected ExitStatus doTaskletProcessing(Tasklet tasklet, + StepExecution stepExecution) throws Exception { return tasklet.execute(); }