Retry on spring batch writer

时间:2017-08-30 20:52:10

标签: spring spring-batch

I have a spring batch using retry on the writer. The writer looks like this:

module HomeHelper
  def result
    "{'var1':'test'}".html_safe
  end
end

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 个答案:

答案 0 :(得分:0)

使用spring jdbc,您将使用事务管理器。使用它来控制设置事务边界并在更新查询后提交它。

另请参阅this示例,其中介绍了如何使用事务管理器。

答案 1 :(得分:0)

不要以这种方式控制重试。使用Spring Batch的重试逻辑。 Spring Batch将回滚事务并以安全的方式重试。您重试的方式是,重试不与事务协调,因此您将获得重复记录(因为事务未回滚)。