Is there any way to persist some data in database after an exception occurs in ItemWriter in spring batch?

泄露秘密 提交于 2019-12-11 08:29:26

问题


I want to persist some data after an exception occurs in ItemWriter step. But if I am not incorrect, in this case a rollback would happen and so this persisting logic won't work.Is there any way to achieve this?

Right now, I am implementing itemWriteListener and all the persisting logic is written inside onWriteError method. This logic only concerns to change state of some entities to error.


回答1:


You'll want to use a separate connection that does not participate in the transaction. For example, inject a DataSource into your listener and construct your own JdbcTemplate with an independent connection. By doing that, the writes should not be rolled back.




回答2:


If you mark the onWriteError method with

@Transactional(propagation = Propagation.REQUIRES_NEW)

...would this work as well?



来源:https://stackoverflow.com/questions/49791072/is-there-any-way-to-persist-some-data-in-database-after-an-exception-occurs-in-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!