SpringBatch does rollback though no-rollback-exception-classes are defined

痴心易碎 提交于 2019-12-10 09:55:47

问题


I got the following job:

<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>

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>

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


回答1:


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



来源:https://stackoverflow.com/questions/22021966/springbatch-does-rollback-though-no-rollback-exception-classes-are-defined

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