BATCH-782: added flag to check that lock is not released before it is acquired

This commit is contained in:
dsyer
2008-08-14 12:34:45 +00:00
parent 4788c7cbf3
commit 17e87e0f2c
3 changed files with 39 additions and 3 deletions

View File

@@ -39,7 +39,7 @@ $ mvn archetype:create -DgroupId=com.mycompany -DartifactId=batch \
<link>
<name>archetypes-pom.xml</name>
<type>1</type>
<location>SPRING_BATCH/archetypes/pom.xml</location>
<locationURI>SPRING_BATCH/archetypes/pom.xml</locationURI>
</link>
</linkedResources>
</projectDescription>

View File

@@ -243,6 +243,8 @@ public class ItemOrientedStep extends AbstractStep {
TransactionStatus transaction = transactionManager.getTransaction(transactionAttribute);
boolean locked = false;
try {
try {
@@ -265,7 +267,8 @@ public class ItemOrientedStep extends AbstractStep {
// to synchronize changes to the step execution (at a
// minimum).
try {
synchronizer.lock(stepExecution);
synchronizer.lock(stepExecution);
locked = true;
}
catch (InterruptedException e) {
stepExecution.setStatus(BatchStatus.STOPPED);
@@ -345,7 +348,11 @@ public class ItemOrientedStep extends AbstractStep {
throw e;
}
finally {
synchronizer.release(stepExecution);
// only release the lock if we acquired it
if (locked) {
synchronizer.release(stepExecution);
}
locked = false;
}
// Check for interruption after transaction as well, so that

View File

@@ -141,6 +141,35 @@ public class StepExecutorInterruptionTests extends TestCase {
}
public void testLockNotReleasedIfChunkFails() throws Exception {
step.setItemHandler(new SimpleItemHandler(new AbstractItemReader() {
public Object read() throws Exception {
throw new RuntimeException("Planned!");
}
}, itemWriter));
step.setSynchronizer(new StepExecutionSynchronizer() {
private boolean locked = false;
public void lock(StepExecution stepExecution) throws InterruptedException {
locked = true;
}
public void release(StepExecution stepExecution) {
assertTrue("Lock released before it is acquired", locked);
}
});
try {
step.execute(stepExecution);
fail("Expected planned RuntimeException");
} catch (RuntimeException e) {
assertEquals("Planned!", e.getMessage());
}
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
}
/**
* @return
*/