0
votes

chunk oriented tasklet flow

I'm using spring-batch and composing 'ChunkOrientedTasklet'.

I saw the image above and realized that the reader and writer operate as a single transaction.

But it wasn't.

Why spring-batch reader and writer use different transactions?

So, reader and writer use different persistence context.

As a result, the select query occurs twice. 1 time for each page unit in the reader, 1 time for each merge in the writer (Because the transaction is different, the entity is not fetched from the persistence context, and a new entity is fetched by querying the DB)

Select queries continue to occur in Writer in inserts, updates, and unchanged situations

reader - QuerydslPagingItemReader (wrapped with JpaPagingItemReader) writer - CustomItemWriter (wrapped with JpaItemWriter)

[Result]

result-1

result-2

result-3

1

1 Answers

0
votes

By default, the JpaPagingItemReader uses a separate transaction so that items can be cached. Here is an excerpt from its Javadoc:

All entity access is performed within a new transaction, independent of any
existing Spring managed transactions.

This can be disabled using the transacted flag.

You can find more details and context here: https://github.com/spring-projects/spring-batch/issues/1610