Spring 批处理:我需要事务注释吗?

Spring Batch: Do I need a Transactional annotation?

我有一个 Spring 批处理应用程序,我配置了一个 SkipPolicy,这样即使无法插入一条记录,整个批处理也不会失败。但行为似乎并非如此。当插入数据库失败时,Postgresql 说事务是“错误的”,所有命令都将中止。因此,以下所有插入数据库的操作也都失败了。 Spring 应该一次重试事务,我想?

所以我们想知道问题是不是因为我们用@Transactional 标记了服务方法?我应该让 Spring 批量控制交易吗?这是我的工作配置:

<bean id="stepScope" class="org.springframework.batch.core.scope.StepScope">
    <property name="autoProxy" value="true"/>
</bean>

<bean id="skipPolicy" class="com.company.batch.common.job.listener.BatchSkipPolicy"/>

<bean id="chunkListener" class="com.company.batch.common.job.listener.ChunkExecutionListener"/>

<batch:job id="capBkdnJob">
    <batch:step id="capStep">
        <tasklet throttle-limit="20">
            <chunk reader="CapReader"  processor="CapProcessor" writer="CapWriter" commit-interval="50"
                   skip-policy="skipPolicy" skip-limit="10">
                <batch:skippable-exception-classes>
                    <batch:include class="com.company.common.exception.ERDException"/>
                </batch:skippable-exception-classes>
            </chunk>
            <batch:no-rollback-exception-classes>
                <batch:include class="com.company.common.exception.ERDException"/>
            </batch:no-rollback-exception-classes>
            <batch:listeners>
                <batch:listener ref="chunkListener"/>
            </batch:listeners>
        </tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="batchWorkerJobExecutionListener"/>
    </batch:listeners>
</batch:job>

简短的回答是:否

Spring 默认情况下,批处理将使用定义为 JobRepository 一部分的事务管理器。这将允许它在遇到错误时回滚整个块,然后在其自己的事务中单独重试每个项目。