Home > OS >  Extending RepositoryItemWriter does not commit delete rows so that the next step sees the rows
Extending RepositoryItemWriter does not commit delete rows so that the next step sees the rows

Time:04-05

I am attempting to use a RepositoryItemWriter to delete Products by their Ids using the RepositoryItemWriter as recommended. I have configured a RepositoryItemWriter as follows:

 @Repository
 public interface ProductRepository extends CrudRepository<Product,Long> {
 }

Next, I specify a RepositoryItemWriter bean as follows:

 class ProductRepositoryItemWriter extends RepositoryItemWriter<Product>{
   private CrudRepository productRepository;
    
    ProductRepositoryItemWriter(CrudRepository productRepository) {
   
    super.setRepository(this.productRepository =                                                      
  productRepository
                                                          
    } 

      @Transactional
      @Override
       protected void doWrite(List<? extends Product> products) {                   
      this.productRepository.deleteAll(products);      
  }

} My step looks like this:

public Step processStep(@Qualifier("jpaTransactionManager") final PlatformTransactionManager jpaTransactionManager) {
    return stepBuilderFactory.get("processStep")
            .transactionManager(jpaTransactionManager)
            .chunk(120)
            .reader(productJpaItemReader)
            .writer((ItemWriter)productRepositoryWriter)
            .build();
   }

I see the deletes occurring but the products are not deleted so that the next step fails to insert the products. That step follows the delete step like this on("COMPLETED").to("uploadStep).end()

The uploadStep fails because rows are still there so I get a ConstraintsViolationException. The doWrite() is annotated with a @Transactional . Why are the rows not deleted? Please does anyone have any ideas figure out this issue?

CodePudding user response:

I thought I solved the but it does not work

CodePudding user response:

You need to remove @Transactional on your doWrite method. The writer is already executed in a transaction driven by Spring Batch.

  • Related