简体   繁体   English

Spring 批量回滚完整步骤而不是块方向的当前事务

[英]Spring Batch Rollback full Step instead of current transaction in chunk orientation

I want to read items from the file and insert it into the table.我想从文件中读取项目并将其插入到表中。 For that, I am using FlatFileItemReader and JdbcBatchItemWriter .为此,我使用FlatFileItemReaderJdbcBatchItemWriter And I am using Chunk Orientation in the step.我在步骤中使用了 Chunk Orientation。 So my step will look like this.所以我的步骤看起来像这样。

private Step createPoliciesStep() {
        return stepBuilderFactory.get("CreatePoliciesStep")
                .<PolicyDTO, PolicyDTO>chunk(2)
                .reader(createPoliciesReaderFunc())
                .writer(createPoliciesWriterFunc())
                .listener(createPoliciesWriteListener)
                .listener(createPoliciesStepListener)
                .build();
}

I gave commit-interval is 2. So every 2 records commit will happen.我给的提交间隔是 2。所以每 2 条记录提交就会发生。 For Example, while reading 4th record some exception is throwing.例如,在读取第 4 条记录时抛出一些异常。 At that time 3rd and 4th records were not inserted, that is absolutely fine.当时没有插入第 3 条和第 4 条记录,那绝对没问题。 I want to rollback 1st and 2nd records (which commits on the previous transactions) on my table.我想回滚我表上的第一条和第二条记录(在以前的事务中提交)。

What I am expecting is before step Begin the transaction.我期待的是在Begin交易之前。 After step should commit the transaction.步骤之后应commit事务。 In between any exception should rollback the entire step.在任何异常之间应该回滚整个步骤。

We have one simple way, In case of any failure on my next step, I can delete those entries.我们有一个简单的方法,万一我的下一步失败了,我可以删除那些条目。 But I felt it is not a good way to do.但我觉得这不是一个好方法。

I tried by adding the transaction manager of DataSourceTransactionManager and transaction attributes but not working.我尝试通过添加DataSourceTransactionManager的事务管理器和事务属性但没有工作。 After adding my step looks like this添加我的步骤后看起来像这样

@Bean
PlatformTransactionManager policyTransactionManager(@Qualifier("mysqlDataSource")DataSource dataSource) {
       return new DataSourceTransactionManager(dataSource);
}

private Step createPoliciesStep() {
        log.info("Create Policies Step Initiated ");
        DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
        attribute.setPropagationBehavior(Propagation.REQUIRED.value());
        attribute.setIsolationLevel(Isolation.DEFAULT.value());
        return stepBuilderFactory.get("CreatePoliciesStep")
                .transactionManager(policyTransactionManager)
                .<PolicyDTO, PolicyDTO>chunk(2)
                .reader(createPoliciesReaderFunc())
                .writer(createPoliciesWriterFunc())
                .listener(createPoliciesWriteListener)
                .transactionAttribute(attribute)
                .listener(createPoliciesStepListener)
                .build();
}

Can anyone suggest some good method to rollback the full Step.任何人都可以建议一些好的方法来回滚整个步骤。 Thanks in advance!!!!提前致谢!!!!

Transaction boundaries in Spring Batch are defined around chunks, not steps. Spring 批处理中的事务边界是围绕块而不是步骤定义的。 So it is not possible to configure a transaction for the entire step.所以不可能为整个步骤配置一个事务。

A common pattern for your use case is to add a separate step that, based on the exit status of the previous step (success or failure), does a "compensating action" which is rolling back the changes of the previous step in your case.您的用例的一个常见模式是添加一个单独的步骤,该步骤基于上一步的退出状态(成功或失败)执行“补偿操作”,即回滚您案例中上一步的更改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM