The Commit Interval

如前所述,步骤使用提供的 PlatformTransactionManager 定期提交读取和写入的项目。使用 commit-interval 值 1,则在写入每个单独的项目后提交。在许多情况下,这并不理想,因为开始和提交事务非常昂贵。理想情况下,最好在每个事务中处理尽可能多的项目,这完全取决于要处理的数据类型和步骤交互的资源。出于此原因,可以配置在提交中处理的项目数。

As mentioned previously, a step reads in and writes out items, periodically committing by using the supplied PlatformTransactionManager. With a commit-interval of 1, it commits after writing each individual item. This is less than ideal in many situations, since beginning and committing a transaction is expensive. Ideally, it is preferable to process as many items as possible in each transaction, which is completely dependent upon the type of data being processed and the resources with which the step is interacting. For this reason, you can configure the number of items that are processed within a commit.

Java

以下示例显示了一个 step,其 taskletcommit-interval 值为 10(在 Java 中定义):

The following example shows a step whose tasklet has a commit-interval value of 10 as it would be defined in Java:

Java Configuration
@Bean
public Job sampleJob(JobRepository jobRepository, Step step1) {
    return new JobBuilder("sampleJob", jobRepository)
                     .start(step1)
                     .build();
}

@Bean
public Step step1(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
	return new StepBuilder("step1", jobRepository)
				.<String, String>chunk(10, transactionManager)
				.reader(itemReader())
				.writer(itemWriter())
				.build();
}
XML

以下示例显示了一个 step,其 taskletcommit-interval 值为 10(在 XML 中定义):

The following example shows a step whose tasklet has a commit-interval value of 10 as it would be defined in XML:

XML Configuration
<job id="sampleJob">
    <step id="step1">
        <tasklet>
            <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/>
        </tasklet>
    </step>
</job>

在前一个示例中,每个事务处理 10 个项目。在处理开始时,事务已启动。此外,每次对 ItemReader 调用 read 时,都会增加计数器。当达到 10 时,聚合项目的列表将传递给 ItemWriter,事务将提交。

In the preceding example, 10 items are processed within each transaction. At the beginning of processing, a transaction is begun. Also, each time read is called on the ItemReader, a counter is incremented. When it reaches 10, the list of aggregated items is passed to the ItemWriter, and the transaction is committed.