Search code examples
javaoraclejdbcresultset

ResultSet.getRow() displays row number 0 instead of x


We recently upgraded the Oracle JDBC driver from ojdbc5 (version 11gr2) to ojdbc8 (21.3.0.0) and we noticed a different behavior that we cannot explain.

boolean stillDataToProcess = true;
int bufferSize = 20000;

while (stillDataToProcess) {
    ResultSet rs = getData();

    for (int i=0; i< bufferSize; i++) {
        if (rs != null && rs.next()) {
            // some stuff
        } else {
            stillDataToProcess = false
            break;
        }
    }

    log.info("row number : " + rs.getRow());

    // some stuff
}

logs before upgrade

row number : 20000
row number : 40000
row number : 60000
row number : 80000
row number : 100000
row number : 120000
row number : 124851

logs after upgrade

row number : 20000
row number : 40000
row number : 60000
row number : 80000
row number : 100000
row number : 120000
row number : 0

Solution

  • The JavaDoc for ResultSet.getRow() says the following about the value this method returns:

    the current row number; 0 if there is no current row

    Once rs.next() has been called and returns false, arguably there is no current row any more, because you've gone off the end of the collection of rows in the result set. So as far as I can see, 0 is the correct value to return in this situation.

    Why the old version of the JDBC driver returned the row number of the last row when the result set was exhausted I can't say. I had a look for a changelog for this driver, but I couldn't easily find one.

    It's unfortunate that you've lost a convenient way to get a count of the number of rows processed. It's still possible to determine this count in some other way, but it'll just require a little more work on your side.