0

I have this ItemReader:

@Bean
public JpaPagingItemReader<GWBillingDetails> itemReaderUpdPCJPA() {

        JpaPagingItemReader<GWBillingDetails> reader = new JpaPagingItemReader();
        reader.setName("jpaPagingUpdPCReader");
        reader.setEntityManagerFactory(entityManagerFactory.unwrap(SessionFactory.class));
        reader.setPageSize(10);
        reader.setQueryString("Select pc from GWPaymentClaim pc INNER JOIN GWTransazioni tr ON tr.id = pc.id.idTransazione WHERE pc.kbStatus = 'SENT' AND tr.status = 'P'");
        reader.setSaveState(true);
        return reader;
}

In its ItemProcessor, i have some elaboration logic in which under a certain condition, it can be made an update to entity in DB. Assuming that we have 21 records read by ItemReader (so then they are divided into 3 pages by ItemReader) and only one record of first page satisfies this condition (so this record is updated), ItemReader inexplicably skips the first record on the second page. Instead if records are two, then item reader skips the first two records on the second page, etc... .

Can anyone tell me why please?

Enzo
  • 1
  • Does this answer your question? [Spring batch jpaPagingItemReader why some rows are not read?](https://stackoverflow.com/questions/26509971/spring-batch-jpapagingitemreader-why-some-rows-are-not-read) – Mahmoud Ben Hassine Aug 12 '21 at 08:57

1 Answers1

1

When Spring Batch processes the chunks sequentially, it executes everything from reader to writer for a single chunk, and then continues with the next chunk. This means that the writer will process the first chunk before the reader processes the second chunk.

If the writer changes the input tables of the reader in a way, that adds or removes entries from the query result of the reader, paging won't work out of the box because limit and offset are outdated when the reader processes the second chunk.

You are missing the 11th item, i.e. the 1st item of the second chunk, because the update changed one of the 10 items such that they are no longer contained in the result set. Then, the previously 12th item becomes the 11th item.

Try rewriting the query such that the result set is not changed by the update. If you need to filter, you can e.g. use an ItemProcessor that maps items that shall not be changed to null.

Henning
  • 3,055
  • 6
  • 30