【问题标题】:Spring batch incorrect write skip count issueSpring批处理不正确的写入跳过计数问题
【发布时间】:2017-03-15 07:56:32
【问题描述】:

我是 Spring Batch 的新手,我有一个问题,我的写入跳过计数被视为块的整个计数,而不仅仅是块中的无效记录。

例如,我正在读取 500 条记录,每个块的块大小为 100 条记录。

然后,如果第一个块有 2 条无效记录,则该无效记录之后的所有记录都被称为无效,并带有“无效异常”,因为它们不是无效的。

因此,对于该批次,batch_step_execution 中的 write_skip_count 为 100,而不是 2。

但另一方面,具有无效记录的块会被重新处理,除了两个无效之外,所有记录都正确到达目的地。 功能已实现,但 write_skip_count 错误,导致我们无法显示正确的日志。请提出我在这里缺少的内容。

我可以看到下面的日志,

检查是否重新抛出:count=1

重试策略:count=1

在应用程序异常时启动事务回滚

下面是我们目前尝试的代码sn-p,

<batch:step id="SomeStep">
    <batch:tasklet>
        <batch:chunk reader="SomeStepReader"
            writer="SomeWriter" commit-interval="1000"
            skip-limit="1000" retry-limit="1">
            <batch:skippable-exception-classes>
                <batch:include class="org.springframework.dao.someException" />
            </batch:skippable-exception-classes>
            <batch:retryable-exception-classes>
                <batch:exclude class="org.springframework.dao.someException"/>
            </batch:retryable-exception-classes>
        </batch:chunk>
    </batch:tasklet>
</batch:step>

【问题讨论】:

    标签: java spring spring-batch


    【解决方案1】:

    尝试了一段时间。我发现当写入数据库时​​发生在块中并且该数据库没有事务管理器,特别是当您的批处理作业从一个数据库数据源读取并写入另一个数据库数据源时。

    在这种情况下,批处理会导致整个块失败,并且跳过计数变为块大小。但它稍后会以提交间隔 = 1 处理卡盘,并仅跳过错误记录并处理正确的记录。但是现在跳过写入计数不正确,因为它应该只是不正确的记录计数。

    为避免这种情况,请为您正在写入数据的数据库数据源创建一个事务管理器。

    <bean id="SometransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="SomedataSource" />
    </bean>
    

    然后在所有事务发生的步骤中使用事务管理器,

    <batch:step id="someSlaveStep">
        <batch:tasklet  transaction-manager="SometransactionManager">
            <batch:chunk reader="SomeJDBCReader"
                writer="SomeWriterBean" commit-interval="1000"
                skip-limit="1000">
                <batch:skippable-exception-classes>
                    <batch:include class="java.lang.Exception" />
                </batch:skippable-exception-classes>
                <batch:listeners>  
                          <batch:listener ref="SomeSkipHandler" />  
                </batch:listeners>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
    

    现在这里的写入失败将在一个事务下发生,错误记录将被优雅地处理,只有错误记录在写入跳过计数下记录在批处理表中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多