Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JDBC Lazy-Loaded ResultSet

Is there a way to get a ResultSet you obtain from running a JDBC query to be lazily-loaded? I want each row to be loaded as I request it and not beforehand.

like image 518
Nathaniel Flath Avatar asked Jun 11 '09 22:06

Nathaniel Flath


2 Answers

Short answer:

Use Statement.setFetchSize(1) before calling executeQuery().

Long answer:

This depends very much on which JDBC driver you are using. You might want to take a look at this page, which describes the behavior of MySQL, Oracle, SQL Server, and DB2.

Major take-aways:

  • Each database (i.e. each JDBC driver) has its own default behavior.
  • Some drivers will respect setFetchSize() without any caveats, whereas others require some "help".

MySQL is an especially strange case. See this article. It sounds like if you call setFetchSize(Integer.MIN_VALUE), then it will download the rows one at a time, but it's not perfectly clear.

Another example: here's the documentation for the PostgreSQL behavior. If auto-commit is turned on, then the ResultSet will fetch all the rows at once, but if it's off, then you can use setFetchSize() as expected.

One last thing to keep in mind: these JDBC driver settings only affect what happens on the client side. The server may still load the entire result set into memory, but you can control how the client downloads the results.

like image 180
Matt Solnit Avatar answered Oct 20 '22 23:10

Matt Solnit


Could you not achieve this by setting the fetch size for your Statement to 1?

If you only fetch 1 row at a time each row shouldn't be loaded until you called next() on the ResultSet.

e.g.

Statement statement = connection.createStatement();
statement.setFetchSize(1);
ResultSet resultSet = statement.executeQuery("SELECT .....");
while (resultSet.next())
{
  // process results. each call to next() should fetch the next row
}
like image 3
Mark Avatar answered Oct 21 '22 00:10

Mark