Som Mark Rotteveel nævner i en kommentar til spørgsmålet, cacher MySQL ResultSet-data som standard (også diskuteret i en blogartikel af Ben J. Christensen her ). En tilsyneladende bivirkning af denne caching er, at MySQL Connector/J vil "opgradere" et TYPE_FORWARD_ONLY ResultSet, så det rent faktisk kan rulles:
Statement s = dbConnection.createStatement(
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = s.executeQuery("SELECT * FROM testdata");
rs.last();
System.out.println(String.format("Current row number: %d", rs.getRow()));
rs.previous();
System.out.println(String.format("Current row number: %d", rs.getRow()));
vises
Current row number: 3
Current row number: 2
Ifølge blogartiklen citeret ovenfor er måden at forhindre caching og "streame" ResultSet-dataene på at bruge Statement.setFetchSize
:
Statement s = dbConnection.createStatement(
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
s.setFetchSize(Integer.MIN_VALUE);
ResultSet rs = s.executeQuery("SELECT * FROM testdata");
rs.next();
System.out.println("Data from first row: " + rs.getString(2));
System.out.println("now let's try rs.last() ...");
try {
rs.last();
System.out.println("... Okay, done.");
} catch (Exception e) {
System.out.println("... Exception: " + e.getMessage());
}
resulterer i
Data from first row: Gord
now let's try rs.last() ...
... Exception: Operation not supported for streaming result sets