适用版本
OceanBase 数据库所有版本。
问题现象
Oracle 模式下,应用使用 setFetchSize(Integer.MIN_VALUE) 报错 invalid fetch size。具体报错信息为:
SQLException: (conn=26835) invalid fetch size
SQLState: 42000
VendorError: -1
java.sql.SQLSyntaxErrorException: (conn=26835) invalid fetch size
at com.oceanbase.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:108)
at com.oceanbase.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:209)
at com.oceanbase.jdbc.OceanBaseStatement.setFetchSize(OceanBaseStatement.java:1390)
at basicJDBCTest.TestProfileSQL.testQuery(TestProfileSQL.java:127)
at basicJDBCTest.TestProfileSQL.main(TestProfileSQL.java:102)
代码样例:
PreparedStatement st1 = null;
String querySql1 = null;
querySql1 = String.format("select c1, c2, c3 from test_cursor where c1 > ? and c1 < ? order by c1");
st1 = conn.prepareStatement(querySql1, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
st1.setFetchSize(Integer.MIN_VALUE);
问题原因
MySQL 模式支持使用 Integer.MIN_VALUE 来设置 setFetchSize,Oracle 模式为了兼容 Oracle 数据库的用法,setFetchSize() 里只能指定 >0 的值。
解决办法
在 setFetchSize() 里指定 >0 的值,同时在连接串中指定 useCursorFetch=true。
conn = DriverManager.getConnection("jdbc:oceanbase://localhost/?useCursorFetch=true", "user", "s3cr3t");
stmt = conn.createStatement();
stmt.setFetchSize(100);