Spring 批量失败侦听器回滚

Spring Batch Failure Listener Rollback

我正在扩展 Spring 批处理的 ItemListenerSupport 并覆盖 onProcessError() 以处理错误状态。我的问题是,作为我处理的一部分,我希望能够写入数据库,但它似乎被包含在批处理的事务中并被回滚。有没有办法让它在之前进行回滚并允许 onProcessError() 更新数据库?或者我应该用其他方式处理这个问题吗?

事实证明它就在文档中。需要添加我自己的交易。

Remember that if your listener does anything in an onError() method, it will be inside a transaction that is going to be rolled back. If you need to use a transactional resource such as a database inside an onError() method, consider adding a declarative transaction to that method (see Spring Core Reference Guide for details), and giving its propagation attribute the value REQUIRES_NEW.