I have a table which has around 5 million records. I want to read all the records from this table and do some processing on them. Now I want to query these records in batches say 1000 in one go, process them and fetch next 1000 records and so on.
However JDBCTemplate.query method only returns List containing all the records in the table. Obviously I can not have 5 million records in memory.
Is there a way address my problem using Spring JDBC? Underlying database is going to be DB2 if that helps.
Read the javadoc of JdbcTemplate. There are plenty of other methods, also named query()
, that don't return a list, and take a RowCallbackHandler
or a ResultSetExtractor
as argument. Use these ones.
To set the number of rows fetched at once by the resultset, override applyStatementSettings()
and call Statement.setFetchSize()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With