How to hide implementation of JDBC while processing large query in client's code? I have client's methods that processes a large SQL query. I wouldn't like to do that through getting whole query result to memory, but process it in batches. It entails with exposing JDBC API to client's code.  I think that this code could be refactored much better.

    //client's method
    m_jdbcManager = JdbcManagerUtil.getInstance();
    m_tupleManager = DbTupleManager.getInstance();
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    try {
        connection = m_jdbcManager.getConnection();
        connection.setAutoCommit(false);
        statement = connection
                .prepareStatement(Statements.SELECT_RIGHT_TUPLES);
        statement.setInt(1, id);
        statement.setFetchSize(JdbcManagerUtil.FETCH_SIZE);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Tuple tuple = (Tuple) m_tupleManager
                    .readObject(resultSet);
            //process tuple, invokes methods from client class 
        }
        connection.setAutoCommit(true);
        JdbcManagerUtil.closeEverything(connection, statement, resultSet); 
    } catch (&lt;exceptions&gt;) { 
    }


I tried to expose method that returns ResultSet, but after processing, there must be released resources Connection, PreparedStatement and ResultSet.
JDBC 4 and PostreSQL database.
Your assumption that the entire dataset is read in to memory is not necessarily accurate. The JDBC clients I have worked with (DB2, MS-SQLServer, Sybase, Oracle, etc.) each default to, or have an option to limit the size of the client-side buffer. You only have a small amount of data on the actual client, and the ResultSet process fetches more data when you iterate through it.

There is no need to add another level of buffering. Are you sure the data is all being returned? Have you inspected the configuration options for your JDBC Client?