Mysql流结果集和jOOQ fetchLazy(Mysql streaming result set and jOOQ fetchLazy)

通常,当我想使用Mysql查询大型结果集时,我写这个(取自这个答案 ):

stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(Integer.MIN_VALUE);

现在我使用的是jOOQ 2.0.5,但我无法达到相同的效果。

我试过没有运气调用fetchLazy ,它将整个结果集加载到内存中:

Cursor<Record> result = query.fetchLazy(Integer.MIN_VALUE);



Normally when I want to query large result set using Mysql I write this (taken from this answer):

stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(Integer.MIN_VALUE);

Now I'm using jOOQ 2.0.5 and I can't achieve the same result.

I've tried calling fetchLazy with no luck, it loads the entire result set in memory:

Cursor<Record> result = query.fetchLazy(Integer.MIN_VALUE);

As a workaround I can get the sql query using query.getSQL() and create a suitable Statement to execute it.

Is there another way to have stream result sets using jOOQ?


根据JDBC规范, Integer.MIN_VALUE不是Statement.setFetchSize()方法的有效参数:

为JDBC驱动程序提供一个提示,指示当此Statement生成的ResultSet对象需要更多行时,应从数据库中提取的行数。 如果指定的值为零,则忽略提示。 默认值为零。



抛出 :SQLException - 如果发生数据库访问错误,则在关闭的Statement上调用此方法,或者不满足条件rows> = 0。

实现可能会为负提取大小抛出SQLException 。 因此,jOOQ不接受小于0的参数。您应该尝试使用1的提取大小。 与此同时,jOOQ的下一个版本可能会破坏JDBC标准,以支持这个记录的MySQL功能 : (在jOOQ 2.2.0中实现)

According to the JDBC specs, Integer.MIN_VALUE is not a valid argument for the Statement.setFetchSize() method:

Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects genrated by this Statement. If the value specified is zero, then the hint is ignored. The default value is zero.


rows the number of rows to fetch

Throws: SQLException - if a database access error occurs, this method is called on a closed Statement or the condition rows >= 0 is not satisfied.

Implementations may throw a SQLException for negative fetch sizes. Hence, jOOQ doesn't accept parameters less than 0. You should try using a fetch size of 1 instead. In the mean time, the next release of jOOQ might be able to break the JDBC standard, to support this documented MySQL feature : (implemented in jOOQ 2.2.0)