尽管定义了 no-rollback-exception-classes,但 SpringBatch 会回滚 [英] SpringBatch does rollback though no-rollback-exception-classes are defined

查看:69
本文介绍了尽管定义了 no-rollback-exception-classes,但 SpringBatch 会回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下工作:

<batch:job id="importCardsJob" job-repository="jobRepository">
    <batch:step id="importCardStep">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="cardItemReader" writer="cardItemWriter"
                commit-interval="5" skip-limit="10">
                <batch:skippable-exception-classes>
                    <batch:include class="java.lang.Throwable" />   
                </batch:skippable-exception-classes>
            </batch:chunk>
            <batch:no-rollback-exception-classes>
                <batch:include class="job.batch.exceptions.BatchImportException"/>
            </batch:no-rollback-exception-classes>
            <batch:listeners>
                <batch:listener ref="skipListener" />
            </batch:listeners>
        </batch:tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="authenticationJobListener" />
        <batch:listener ref="jobListener" />
    </batch:listeners>
</batch:job>

我正在从 csv 导入某些数据.如果出现任何问题,应该跳过它并阅读下一行.这就是为什么我添加了可跳过的异常类.我的作者将卡片存储到数据库中,包括一捆 5 张卡片(由commit-interval 定义).我不想每一步只保存一张卡,这就是为什么我添加了 no-rollback-exception-classes.如果 csv 包含任何损坏的数据,则应抛出 BatchImportException(由我编写)并继续导入.默认情况下,从 ItemWriter 抛出的任何异常都会导致 Step 控制的事务回滚.但我不想要这个.这就是我添加 no-rollback-exception-classes 的原因.可悲的是,在抛出 BatchImportException 之后,SpringBatch 仍然会进行回滚.我什至尝试了以下方法:

I am importing certain data from a csv. If any problem occurrs, it should be skipped and the next line should be read. This is why i added the skippable-exception-classes. My writer stores cards to the db including a bundle of 5 cards (defined by comit-interval). I don't want to save only one single card every step, that's why i added the no-rollback-exception-classes. If the csv contains any corrupted data, a BatchImportException (written by me) should be thrown and the import should go on. By default any exceptions thrown from the ItemWriter will cause the transaction controlled by the Step to rollback. But i don't want this. That's why i added the no-rollback-exception-classes. Sadly, after a BatchImportException is thrown, SpringBatch still does a rollback. I even tried the following:

<batch:no-rollback-exception-classes>
    <batch:include class="java.lang.Throwable"/>
</batch:no-rollback-exception-classes>

但即使在这种情况下,SpringBatch 也会进行回滚.这是为什么?

But even in this case, SpringBatch does a rollback. Why is that?

推荐答案

也许 https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/ 文章可以帮助.
如果错误是从写入器或读取器/处理器抛出的,SB 会采取不同的操作,在您的情况下,可能是执行回滚,SB 再次循环以检查哪个项目是导致错误的原因.也看看 帮助配置无回滚异常类

Maybe https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/ article can help.
SB take different action if error is throwed from writer or from reader/processor and in your case can be that a rollback is performed and SB do loop again to check which item was the cause of error. Also take a look at HELP WITH NO-ROLLBACK-EXCEPTION-CLASSES CONFIGURING

这篇关于尽管定义了 no-rollback-exception-classes,但 SpringBatch 会回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆