简体   繁体   中英

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 . And I am using Chunk Orientation in the step. 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. For Example, while reading 4th record some exception is throwing. At that time 3rd and 4th records were not inserted, that is absolutely fine. 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. After step should commit the transaction. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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