I'm using a JdbcRowSet to retrieve a data set that could be very large (1M+ rows returned).
Because the result set could be so large I do not wish to hold it in memory. I wish to use the JdbcRowSet as a connected RowSet and fetch rows from the database as needed, rather than using the JdbcRowSet as a disconnected RowSet which would hold all rows in memory.
Unfortunately when using the below code to initialize my JdbcRowSet, the entire RowSet is pulled into the heap, which causes OutOfMemoryErrors and takes the application down.
How can I prevent the JDBC driver or JdbcRowSet from forcibly loading the entire result set into memory, but rather only fetching (per the example below) 100 rows into memory at a time?
I am using Spring to wire the application generally, and Spring JDBC templates throughout my data layer. Database is Oracle 11g.
JdbcRowSet jdbcRowSet = new com.sun.rowset.JdbcRowSetImpl(jdbcTemplate.getDataSource().getConnection());
jdbcRowSet.setConcurrency(ResultSet.CONCUR_READ_ONLY);
jdbcRowSet.setReadOnly(true);
jdbcRowSet.setCommand(sql);
jdbcRowSet.setString(1, value);
jdbcRowSet.execute();
jdbcRowSet.setFetchDirection(ResultSet.FETCH_FORWARD);
jdbcRowSet.setFetchSize(100);
(please don't encourage me to change my query so that fewer rows are returned; I assure you that this is a business requirement, is non-optional, etc)