2

I created a spring batch job by using FlatFileItemReader which reads data from a delimited file and then write to DB by using JdbcBatchItemWriter. And my setp configuration is like below.

<batch:step id="step1">
        <batch:tasklet>
            <batch:chunk reader="fileReader" 
                writer="dbWriter" commit-interval="100">
            </batch:chunk>
        </batch:tasklet>
</batch:step>

The configuration above is opening separate transactions for each 100rows, so if a failure occurs before completion of tasklet(step-1) then I can't revert the previously committed rows. Is there a way to run the entire tasklet in a single transaction?.

P.S: I am using MapJobRepositoryFactoryBean as job repository, don't want to create meta tables in database for restarting.

Raghava Ch
  • 77
  • 2
  • 10

2 Answers2

2

(Have been some time since I last used Spring Batch, I wish my understanding is still valid :P ) Instead of using chuck-oriented tasklet, you can simply create one simple tasklet. By default, a simple tasklet will run in a single transaction. Given that you have already built reader and writer, you can write a tasklet which mimics a chuck-oriented step is doing (just pseudo-code to show you the idea):

public class ReaderWriterTasklet<T> implements Tasklet {
    private ItemReader<T> reader;
    private ItemWriter<T> writer;
    // and corresponding setters

    public RepeatStatus execute(StepContribution contribution,
                     ChunkContext chunkContext) {
        List<T> chunk = new LinkedList<T>();
        while (true) {
            T item = reader.read();
            if (item == null) {
                break;
            } else {
                chunk.add(item);
            }
        }
        writer.write(chunk);
        return RepeatStatus.FINISHED;
    }
}

(I believe you should already know how to define a step running a tasklet right? I'll skip that then)

Another dirty way is keep using the chunk, and set the commit-interval to Integer.MAX_VALUE. By doing so, the chunk oriented step will keep getting item from reader until it reach the end, and write to writer in one big chunk, which all happens within 1 transaction.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
1

In Spring batch , there will be always only one transaction at a time for a job .

Notice the below diagram , as we can see transaction is opened at the beginning of the step and it is committed at the end of the step .

Spring Batch transaction management

Image Source

In fact ,one of the main advantages of using spring batch is that we as a developer don have to worry about transaction management . Even if there is a failure , it will automatically rollback then entire uncommitted transaction .

DevG
  • 474
  • 4
  • 16
  • When we use spring batch in project and @EnableBatchProcessing annotations will create its own transactions for its default tables. Hence if you want to perform any insert, update or delete in our application tables. We have to mention JpaTransactionManager instead of default spring batch transactions. – Bharathiraja Dec 29 '20 at 19:01