简体   繁体   English

在面向读写块的过程中,Spring Batch 在哪里提交?

[英]In a read-write chunk oriented process where does Spring Batch commit?

On my java project, I am having a read ( FlatFileItemReader ) - write ( JdbcBatchItemWriter ) chunk oriented process using Spring boot.在我的 java 项目中,我正在使用 Spring Boot 进行读取( FlatFileItemReader )-写入( JdbcBatchItemWriter )面向块的过程。 Using HikariCp datasources.使用HikariCp数据源。

Looking into the Spring batch jdbc writer we can find that part :查看 Spring 批处理 jdbc 编写器,我们可以找到该部分:

               //some code
                @Override
                public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    for (T item : items) {
                        itemPreparedStatementSetter.setValues(item, ps);
                        ps.addBatch();
                    }
                    return ps.executeBatch();
                }
              //some code

So basically here if my Hikari datasource has setAutocommit parameter to true it means that after ps.executeBatch() my database will be updated.所以基本上在这里,如果我的 Hikari 数据源将setAutocommit参数设置为true,则意味着在ps.executeBatch()我的数据库将被更新。

First question here, on PreparedStatement.executeBatch() how does it process exactly?这里的第一个问题,关于PreparedStatement.executeBatch()它是如何处理的? Does it commit every sql statement or at the end of every sql statements?它是提交每个 sql 语句还是在每个 sql 语句的末尾?

In the case where setAutocommit parameter is on false , after ps.executeBatch() it should not be updated.setAutocommit参数为false的情况下,在ps.executeBatch()之后不应更新。

Since Spring batch should manage the transaction and so the commit.由于 Spring 批处理应该管理事务等提交。 I tried to find where does it commit for a better understanding on an issue that I have.我试图找到它在哪里提交以便更好地理解我遇到的问题。 Looking to ChunkOrientedTasklet , SimpleChunkProcessor and TransactionTemplate I could not find where in the process it does the commit.寻找ChunkOrientedTaskletSimpleChunkProcessorTransactionTemplate我找不到它在进程中提交的位置。

So my second question is, where does Spring batch exactly commit on a chunk oriented process?所以我的第二个问题是, Spring 批处理在面向块的过程中究竟在哪里提交?

EDIT : Using Spring batch 3.0.7编辑:使用 Spring 批处理 3.0.7

EDIT : It seems processed in AbstractPlatformTransactionManager.processCommit(DefaultTransactionStatus status) but still can not understand how.编辑:它似乎在AbstractPlatformTransactionManager.processCommit(DefaultTransactionStatus status)处理,但仍然无法理解。

Your autocommit parameter is irrelevant, Spring will manage things and override that as it sees fit (and it does see it fit).您的自动提交参数无关紧要,Spring 将管理事物并在它认为合适时覆​​盖它(它确实认为合适)。

You can set the commit interval to define how often you want to commit.您可以设置提交间隔来定义您想要提交的频率。

Whether you should change it depends entirely on how you intend to batch, whether your execution can fail, whether those cases should be skipped or retried and other such things.您是否应该更改它完全取决于您打算如何批处理,您的执行是否会失败,是否应该跳过或重试这些情况以及其他类似的事情。

Regarding your question on "when is it committed?", per the Spring Batch docs :关于“什么时候提交?”的问题,根据Spring Batch 文档

5.1 Chunk-Oriented Processing 5.1 面向块的处理

Spring Batch uses a 'Chunk Oriented' processing style within its most common implementation. Spring Batch 在其最常见的实现中使用“面向块”的处理风格。 Chunk oriented processing refers to reading the data one at a time, and creating 'chunks' that will be written out, within a transaction boundary.面向块的处理是指在事务边界内一次读取一个数据,并创建将被写出的“块”。 One item is read in from an ItemReader, handed to an ItemProcessor, and aggregated.从 ItemReader 读入一项,交给 ItemProcessor 并聚合。 Once the number of items read equals the commit interval, the entire chunk is written out via the ItemWriter, and then the transaction is committed.一旦读取的项数等于提交间隔,则通过 ItemWriter 写出整个块,然后提交事务。

在此处输入图片说明

Below is a code representation of the same concepts shown above:下面是上述相同概念的代码表示:

 List items = new Arraylist(); for(int i = 0; i < commitInterval; i++){ Object item = itemReader.read() Object processedItem = itemProcessor.process(item); items.add(processedItem); } itemWriter.write(items);

Hence, if you're going to use Spring Batch, you shouldn't be messing with setting auto-commit, as this is something you should trust the framework to handle for you.因此,如果您打算使用 Spring Batch,您不应该搞乱设置自动提交,因为这是您应该信任框架为您处理的事情。 The section immediately below that shows an example of how this is configured:下面的部分显示了如何配置的示例:

5.1.1 Configuring a Step 5.1.1 配置步骤

Despite the relatively short list of required dependencies for a Step, it is an extremely complex class that can potentially contain many collaborators.尽管 Step 所需的依赖项列表相对较短,但它是一个极其复杂的类,可能包含许多协作者。 In order to ease configuration, the Spring Batch namespace can be used:为了简化配置,可以使用 Spring Batch 命名空间:

 <job id="sampleJob" job-repository="jobRepository"> <step id="step1"> <tasklet transaction-manager="transactionManager"> <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/> </tasklet> </step> </job>

The configuration above represents the only required dependencies to create an item-oriented step:上面的配置表示创建面向项目的步骤所需的唯一依赖项:

  • reader - The ItemReader that provides items for processing. reader - 提供待处理项目的 ItemReader。

  • writer - The ItemWriter that processes the items provided by the ItemReader. writer - 处理 ItemReader 提供的项目的 ItemWriter。

  • transaction-manager - Spring's PlatformTransactionManager that will be used to begin and commit transactions during processing.事务管理器 - Spring 的 PlatformTransactionManager 将用于在处理期间开始和提交事务。

  • job-repository - The JobRepository that will be used to periodically store the StepExecution and ExecutionContext during processing (just before committing). job-repository - JobRepository 将用于在处理期间(就在提交之前)定期存储 StepExecution 和 ExecutionContext。 For an in-line (one defined within a ) it is an attribute on the element;对于内联(在 a 中定义的),它是元素上的一个属性; for a standalone step, it is defined as an attribute of the .对于独立步骤,它被定义为 .

  • commit-interval - The number of items that will be processed before the transaction is committed. commit-interval - 在提交事务之前将处理的项目数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个资源的Spring Batch MultiResourceItemReader块提交 - Spring Batch MultiResourceItemReader Chunk Commit Per Resource 使用面向块的处理在Spring批处理远程分块中实现SkippableTasklet - Implement SkippableTasklet in Spring batch remote chunking with Chunk Oriented Processing 如何在Spring Mail Integration中启用IMAPFolder读写模式 - How to enable IMAPFolder read-write mode in Spring Mail Integration Java中的StoreStore memory屏障是否禁止读写重新排序? - Does StoreStore memory barrier in Java forbid the read-write reordering? Spring 批处理 - 即使块引发一些异常,是否有办法提交数据? - Spring Batch - Is there a way to commit data even if the chunk raise some exception? 春季批处理工作| 读/写是否发生在同一线程中? - Spring batch jobs | Does the read/write happen in the same thread? 使用 spring @Transactional 和 AbstractRoutingDataSource 在只读和读写数据库之间切换 - Switching between read-only and read-write databases using spring @Transactional and AbstractRoutingDataSource 为什么 spring/hibernate 只读数据库事务运行速度比读写慢? - Why do spring/hibernate read-only database transactions run slower than read-write? 如何使用Spring以只读和读写方式进行数据库路由 - How to do database routing in read-only and read-write with Spring 如何在Spring Batch的块过程中调用另一个步骤? - How to call another step within an chunk process in Spring Batch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM