【问题标题】:Spring: How to restart transaction in a for-loop?Spring:如何在 for 循环中重新启动事务?
【发布时间】:2019-12-04 17:47:47
【问题描述】:

我有一个 Spring Batch 应用程序,它在 write 步骤期间循环遍历要插入 Postgres 数据库的记录。我们时不时地在循环中得到一个DuplicateKeyException,但不希望整个工作失败。我们记录该记录并希望继续插入以下记录。

但是一旦出现异常,事务就会变成“坏”并且 Postgres 将不再接受任何命令,如 in this excellent post 所述。所以我的问题是,重启交易的最佳方式是什么?同样,我不会重试失败的记录 - 我只想继续循环下一条记录。

这是我的工作配置 xml 的一部分:

<batch:job id="portHoldingsJob">
    <batch:step id="holdingsStep">
        <tasklet throttle-limit="10">
             <chunk reader="PortHoldingsReader"  processor="PortHoldingsProcessor" writer="PortHoldingsWriter" commit-interval="1" />
        </tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="JobExecutionListener"/>
    </batch:listeners>
</batch:job>

感谢您的任何意见!

【问题讨论】:

    标签: spring-data-jpa spring-batch


    【解决方案1】:

    不确定您是否使用 Spring 事务注释来管理事务...如果是这样,也许您可​​以尝试。

       @Transactional(noRollbackFor = DuplicateKeyException.class)
    

    希望对您有所帮助。

    Spring Batch 中没有回滚异常显然被指定为

    <batch:tasklet>
        <batch:chunk ... />
            <batch:no-rollback-exception-classes>
            <batch:include class="MyRuntimeException"/>
        </batch:no-rollback-exception-classes>
    </batch:tasklet>
    

    【讨论】:

    • 完美!这正是我所需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 2021-08-16
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    相关资源
    最近更新 更多