Spring Batch and transactions in ItemWriter

  Kiến thức lập trình

Transactions are already managed in Spring Batch, but I’m wrting a custom ItemWriter, should I manage myself transactions in this case ?

Here’s a piece of my code :

public class DataItemWriter implements ItemWriter<Data> {

    private DataService dataService;

    public DataItemWriter(DataService dataService) {
        this.dataService = dataService;
    }

    @Override
    public void write(Chunk<? extends Data> chunk) throws Exception {
        chunk.getItems()
                .stream()
                .flatMap(List::stream)
                .map(it -> {//doing some stuff})
                .forEach(dataService::save);
        
    }

Basically my DataService encapsulates a DataJpaRepository<Data,Long> from Spring Data

Does this means a new transaction is created for dataService::save call ?

LEAVE A COMMENT