1
votes

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.

2

2 Answers

0
votes

With your spring jdbc you would be using a transaction manager. Use it to take control on setting the transaction boundry and commit it after your update query.

Also look at this example it describes how to use transaction manager.

-1
votes

Don't control retry that way. Use Spring Batch's retry logic. Spring Batch will roll back the transaction and retry in a safe way. The way you are doing your retry, the retry doesn't coordinate with the transaction so you are getting duplicate records (since the transaction wasn't rolled back).