问题
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