We noticed the same problem by setting up some integration tests to see if Spring batch can solve the needs for our organization.
The test is pretty simple and set up like this so maybe you can use it to reproduce the behaviour:
Code:
<job id="integrationTest4" xmlns="http://www.springframework.org/schema/batch">
<step id="useCase4">
<batch:tasklet>
<batch:chunk reader="reader" processor="simpleProcessor" writer="failWriter"
commit-interval="2" retry-limit="3">
<retryable-exception-classes>
<include class="java.io.IOException"/>
</retryable-exception-classes>
</batch:chunk>
</batch:tasklet>
</step>
</job>
The input file only contains 10 records (easier for debugging) so a small commit-interval was chosen.
The reader is a FlatFileItemReader. The writer simply extends FlatFileItemWriter to throw an error every 3 times and looks like this
Code:
public class FailWriter<T> extends FlatFileItemWriter<T> {
private static Log logger = LogFactory.getLog(FailWriter.class);
private static int counter = 1;
@Override
public void write(List<? extends T> items) throws Exception {
counter++;
if (counter%3==0) {
throw new IOException("Randomizer");
}
super.write(items);
}
}
After debugging for ourself, we were able to make it work by changing the following piece of code in FaultTolerantChunkProcessor, line 217 and following:
Code:
if (!processorTransactional && cached != null && count.get() > scanLimit) {
/*
* If there is a cached chunk then we must be
* scanning for errors in the writer, in which case
* only the first one will be written, and for the
* rest we need to fill in the output from the
* cache.
*/
output = cached;
}
but we're not sure about possible consequences.
Is this a possible fix for this problem? If so, can it be implemented for the next spring batch release?