I have a spring batch using retry on the writer. The writer looks like this:
public class MyWriter implements ItemWriter<MyClass> {
@Retryable(maxAttempts=3, backoff=@Backoff(delay=2000))
public void write(List<? extends MyClass> list) throws Exception {
// db operation 1 -- insert query
// some business logic
// db operation 2 -- update query
}
}
If there is an exception in 'db operation 2', retry is called and execution starts from 'db operation 1' again as expected. But 'db operation 1' is an insert query and throws an exception this time as it tries to insert the same record again (and unique constraint on the DB fails). Ultimately, the batch fails after 3 tries and nothing from this step is committed to the database.
Shouldn't 'db operation 1' been rolled back/flushed before retry? Is this expected behavior or something wrong with the implementation?
I am using Spring boot application and spring jdbc.