Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NamedParameterJdbcTemplate batch size

Tags:

java

spring

Is there a way to set the batch size for Spring's NamedParameterJdbcTemplate object?

I ran into some OutOfMemory issues in my project but I was able to resolve it by calling NamedParameterJdbcTemplate in a loop of smaller chunks. But this required some extra effort like deciding the chunk size, breaking a big List into smaller sublists etc.

I was wondering if NamedParameterJdbcTemplate has any such direct way by I can specify the batch size for it. I do not see anything though in the API documentation. But they have something in JDBCTemplate. Now if I have to switch to JDBCTemplate I will have to redo my code :(

Please suggest.

like image 942
deepak Avatar asked Nov 11 '22 22:11

deepak


1 Answers

You can't do this directly with NamedParameterJdbcTemplate, but you can call #getJdbcOperations method via implemented NamedParameterJdbcOperations interface. Its return type, JdbcOperations, is currently implemented only by classic JdbcTemplate and has #batchUpdate method that you need. However, you can't use named parameters in this scenario.

See the example of typical usage from Spring docs.

like image 135
Grade Avatar answered Nov 15 '22 13:11

Grade