Amit Pokhrel
Amit Pokhrel

Reputation: 290

Batch update with certain batch size using NamedParameterJdbcTemplate

Is there an easy way to insert records in a database in certain batch (for e.g. 500) using NamedParameterJdbcTemplate. The .batchUpdate() method don't take batch size as argument. I believe it tries to insert whatever number of records there are in 1 single batch. Below is my code:

private void insertInBatch(List<Map<String, Object>> memberList){
String query = "Insert into table (ID) values (:id)";
namedParameterJdbcTemplate.batchUpdate(query, SqlParameterSourceUtils.createBatch(memberList))
}

MemberList can have many records that I want to insert in batch. In this case, Is there any advantage of inserting members in a single batch vs batch of 500?

Upvotes: 0

Views: 3113

Answers (2)

Sathyam Lokare
Sathyam Lokare

Reputation: 3

@amit I think the only way is to wrap it around an interface and run a loop inside based on the batch size that is required, I was also facing the same issue and my batch updates were timing out > 30 seconds.

If you found a better solution do answer your own question.

Upvotes: 0

mohammedkhan
mohammedkhan

Reputation: 975

No it's not possible. If there is a limit on the number of records you can insert in a single batch in your database (for example Oracle has a limit of 1000), you would have to write something that breaks your collection into more than one collection of that maximum size and run batchUpdate every time.

Upvotes: 1

Related Questions