Given following situation:
JDBC
)The problem here is that all the DAO results are hold in memory which becomes dangerous as the tables grows.
Question: Is there a pattern to read objects from a DAO in a streaming way that the service processes only one element at a time, writes it to the file output stream and then requests the next element from the result set? Is it in general a good idea to use a ResultSet
as a return type of a DAO?
Even if you exposed the results set, which you shouldn't, you still don't get streaming semantics by default. For that you need to do something like this:
stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);
You should encapsulate this within your DAO, you could return a custom iterator to process the results.
See the mysql reference on the topic of streaming results.