Spring batch rollbacks the inserts

混江龙づ霸主 提交于 2020-01-06 05:43:05

问题


Configured the below in my project:

<batch:no-rollback-exception-classes>
        <batch:include class="java.sql.SQLException"/>
        <batch:include class="org.springframework.dao.DuplicateKeyException"/>
       <batch:include class="java.sql.SQLIntegrityConstraintViolationException"/>

</batch:no-rollback-exception-classes>

While loading the file, I have duplicate records, but since I have configured org.springframework.dao.DuplicateKeyException under no-rollback-exception-classes, Spring batch should not rollback the records, but still the records are getting rollbacked. If I remove the DuplicateKeyException from the list, then it is throwing the exception. We are using Spring batch version: 3.0.7.RELEASE

<batch:no-rollback-exception-classes>
        <batch:include class="java.sql.SQLException"/>
        <batch:include class="org.springframework.dao.DuplicateKeyException"/>
       <batch:include class="java.sql.SQLIntegrityConstraintViolationException"/>

</batch:no-rollback-exception-classes>

Records are not expected to rollback, but records are rollbacked.


回答1:


According to your configuration, when a DuplicateKeyException is thrown, Spring Batch will still try to commit the transaction (no rollback), but this commit will fail anyway due to that exception. Spring Batch can't force the database to commit records with duplicate keys or violating integrity constraints.

You need to filter out duplicate items with an ItemProcessor before sending them to the writer.



来源:https://stackoverflow.com/questions/54628388/spring-batch-rollbacks-the-inserts

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