首页 文章

虽然定义了无回滚异常类,但SpringBatch会进行回滚

提问于
浏览
3

我得到了以下工作:

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

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

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

1 回答

相关问题