Java Spring批处理回滚完整步骤,而不是块定向中的当前事务
我想从文件中读取项并将其插入表中。为此,我使用了Java Spring批处理回滚完整步骤,而不是块定向中的当前事务,java,spring,spring-data,spring-batch,spring-transactions,Java,Spring,Spring Data,Spring Batch,Spring Transactions,我想从文件中读取项并将其插入表中。为此,我使用了FlatFileItemReader和JdbcBatchItemWriter。我在步骤中使用了块定向。所以我的脚步看起来像这样 private Step createPoliciesStep() { return stepBuilderFactory.get("CreatePoliciesStep") .<PolicyDTO, PolicyDTO>chunk(2)
FlatFileItemReader
和JdbcBatchItemWriter
。我在步骤中使用了块定向。所以我的脚步看起来像这样
private Step createPoliciesStep() {
return stepBuilderFactory.get("CreatePoliciesStep")
.<PolicyDTO, PolicyDTO>chunk(2)
.reader(createPoliciesReaderFunc())
.writer(createPoliciesWriterFunc())
.listener(createPoliciesWriteListener)
.listener(createPoliciesStepListener)
.build();
}
有谁能建议一些好的方法来回滚整个步骤吗。提前感谢 Spring批处理中的事务边界是围绕块而不是步骤定义的。因此,不可能为整个步骤配置事务
用例的一种常见模式是添加一个单独的步骤,该步骤基于前一步骤的退出状态(成功或失败),执行“补偿操作”,该操作将回滚案例中前一步骤的更改。我看到了一个答案,他们在ItemWriter extends
StepListenerSupport
中进行操作,并在设置autocommit false中覆盖beforeStep()
方法,以及覆盖使事务提交的afterStep()
方法。这样好吗?。我试过了,但是覆盖方法根本没有调用,为什么?。你能说在Javadoc中使用StepListenerSupport
不是很清楚吗。Itemwirter类看起来像这样公共类JDBCWriter扩展StepListenerSupport实现ItemWriter
这是它的引用-
@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();
}