Search code examples
javaspring-batch

Spring batch: Writer approach to update and insert with a commit for each item


My need is to read from a database items to process, have processors to check if each item should be processed according to rules (it retrieves data from tables and completes items with necessary data if it should be processed or return null if it is not to not process the item) and to update and write some data for each item that have been processed. The aim is to do all the update and insert queries for one item, to commit and then to treat the next item.

I set a chunk size of 1 to have a commit for each item because if an item is processed and the corresponding data has been written in database, this item should not be pocessed again if the batch has failed for an other item after and is restarted.

I do not understand how I can use a writer (or a compositeItemWriter) to update and insert in some tables the result of the batch. Indeed, I see that a writer receives the list of items as parameter instead of only one item to do all the update and insert queries item by item. Also, I saw that there is some implementations for ItemWriter (RepositoryItemWriter and JdbcBatchItemWriter for example) and I do not know which one I should use. Also, I do not understand if I can create and call one or some services in the writer to do all the update and insert actions.

Can you help me to have the best approach for the writer part to update, insert and then commit for each item please?


Solution

  • Item writer gets a list of items because it is supposed to write all items gathered in a chunk. But as you set the chunk size to one the list will contain only one item. You can persist just this one and it will be committed immediately.

    And if the processor returns null the writer will not be involved for the particular item.

    You can use JdbcBatchItemWriter even if the size will be one. Or RepositoryItemWriter if you prefer JPA over JDBC.

    Hope it helps.