ARTICLE AD BOX
How do I confirm that JdbcTemplate is fetching the records into the result set in batches of 1000 when setFetchSize(1000) is set on the statement?
I am using PostgreSQL JDBC driver here.
Below is my code:
@Transactional(readOnly = true) private void fetchAndRowCallBackProcessing(CSVPrinter csvPrinter) { jdbcTemplate.query(con -> { con.setAutoCommit(false); // This is not needed since I have used @Transactional PreparedStatement ps = con.prepareStatement( SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY ); ps.setFetchSize(1000); LOGGER.info("jdbcTemplate fetch size - " + jdbcTemplate.getFetchSize()); // Logged -1 LOGGER.info("ps fetch size - " + ps.getFetchSize()); // Logged 1000 return ps; }, rs -> { try { if (rs.getString(1).equals("1740983035847")) // Just for debugging { LOGGER.info("rs fetch size - " + rs.getFetchSize()); // Logged 1000 } csvPrinter.printRecord( rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9)); } catch (IOException e) { e.printStackTrace(); } }); }Although rs.getFetchSize() logs 1000, how can I be sure that the actual batches pulled from DB to the result set are in sizes of 1000, since I see a heap memory spike for over a minute. Ideally, if only 1000 records stay in the memory one time then there shouldn't be much spike. Can it be due to some other objects getting created in that duration?
Note - I am debugging on my local so there are no other incoming requests.
