Question

I have this code, this code basically set limits in the PreparedStatement to avoid loading the whole table data.

PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        if (getDataSheet().isPagingEnabled()) {
            pstmt.setFetchSize(getDataSheet().getPageSize() + 1);
        }

        if (getDataSheet().isDisableTotalRowsCount() && maxRecords >= 0) {
            if (getDataSheet().isPagingEnabled()) {
                pstmt.setMaxRows(getCurrentPageSize());
            } else {
                pstmt.setMaxRows(maxRecords);
            }
        }

        //the rest of code.

    } catch(Exception ex) {
        //handle exception
    }

This code is just working fine for all DB vendors, except one case in Oracle driver if i used the setFetchSize in the PreparedStatement, and the page size was more than 20000 the driver throw an OutOfMemoryError

Caused by: java.lang.OutOfMemoryError: Java heap space
at oracle.jdbc.driver.PhysicalConnection.getCharBuffer(PhysicalConnection.java:7018)
at oracle.jdbc.driver.OracleStatement.prepareAccessors(OracleStatement.java:907)
at oracle.jdbc.driver.T4CTTIdcb.receiveCommon(T4CTTIdcb.java:261)
at oracle.jdbc.driver.T4CTTIdcb.receive(T4CTTIdcb.java:127)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:992)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:791)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:866)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1186)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3387)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3431)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1203)

any idea why is that happening in Oracle? is there any think to do in the DB level? thanks in advanced.

Was it helpful?

Solution

Contrary to what you seem to think, the setFetchSize does not limit the amount of records retrieved, it determines (or hints) how many records the driver should prefetch and cache. The exception is MySQL, which always prefetches all records, unless you set the fetch size to Integer.MIN_VALUE.

The driver is simply retrieving so much rows that memory gets exhausted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top