diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java index 7cca3e0af1..7a4712f647 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java @@ -392,11 +392,6 @@ public Object doInTransaction(TransactionStatus status) { chunkListener.beforeChunk(chunkContext); - // In case we need to push it back to its old value - // after a commit fails... - oldVersion = new StepExecution(stepExecution.getStepName(), stepExecution.getJobExecution()); - copy(stepExecution, oldVersion); - try { try { @@ -422,6 +417,13 @@ public Object doInTransaction(TransactionStatus status) { try { semaphore.acquire(); locked = true; + // In case we need to push it back to its old value + // after a commit fails... + // Here we are sure we get the most up to date version since + // semaphore acquisition garantees that no other thread has modified it + oldVersion = new StepExecution(stepExecution.getStepName(), stepExecution.getJobExecution()); + copy(stepExecution, oldVersion); + } catch (InterruptedException e) { logger.error("Thread interrupted while locking for repository update");