最近在Junit上工作,用hsqldb(2.3.3)测试Dao相关代码。系统使用mybatis进行数据持久化。
在mybatisXXsql.xml中,select语句中有“fetchSize”属性,例如:
<select ... resultSetType="FORWARD_ONLY" fetchSize="-2147483648">
...
</select>
当我们使用hsqldb调用测试sql时,它显示以下异常:
Caused by: java.sql.SQLException: Invalid argument in JDBC call
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCUtil.outOfRangeArgument(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setFetchSize(Unknown Source)
我检查过它似乎不支持内存数据库中的“fetchSize”,任何人都可以提供一些建议我如何测试这个sql部分?
最佳答案
负fetchSize
是not supported通过 JDBC。以下是 setFetchSize
引发的异常的描述:
SQLException - if a database access error occurs, this method is called on a closed Statement or the condition rows >= 0 is not satisfied.
错误的原因是获取大小不正确(堆栈跟踪中的 outOfRangeArgument
也表明了这一点)。
mysql 驱动程序确实支持 Integer.MIN_INT
作为获取大小来指定结果集处于流模式:
The combination of a forward-only, read-only result set, with a fetch size of Integer.MIN_VALUE serves as a signal to the driver to stream result sets row-by-row. After this, any result sets created with the statement will be retrieved row-by-row.
mybatis 本身不允许在映射器配置中对 fetchSize
进行参数化。
您需要使用一些技术来通过 AspectJ
或 PowerMock
覆盖配置。通过 PowerMock
,您可以在调用执行查询的方法之前使用 PowerMock.stub
执行类似的操作:
PowerMock.stub(
PowerMock.method(
org.apache.ibatis.mapping.MappedStatement.class,
"getFetchSize"
)
).toReturn(1);
这样,当 mybatis 创建 Statement
时,它将使用 fetchSize
的模拟值
关于mysql - 如何在内存数据库中设置 fetchSize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53114053/