Skip to content

[Bug]: Connector/J useCursorFetch=true fails with unsupported Prepare flag '1' #27286

Closed

Description

@Ariznawlll

Description

Connector/J 开启服务端游标读取(useCursorFetch=true)后,MatrixOne 在处理 COM_STMT_EXECUTE 时拒绝游标标志 1,查询无法执行,报错:

SQLState=HY000, errorCode=20301
invalid input: unsupported Prepare flag '1'

关闭 useCursorFetch 后,同一个 server-side prepared statement 查询正常返回结果。

Environment

  • Branch: main
  • Commit: 69abff30d1074f856a100e52e53be4127e5af8c2
  • Build: source build, Go 1.26.5, darwin/arm64
  • Deployment: 单进程本地 LOG/TN/CN,独立数据目录,CN SQL 端口 26001
  • Java: JDK 20.0.1
  • Drivers: MySQL Connector/J 8.0.15、8.0.33

Steps to reproduce

  1. 创建测试数据:
CREATE DATABASE jdbc_cursor_bug;
USE jdbc_cursor_bug;
CREATE TABLE cursor_rows(id INT PRIMARY KEY);
INSERT INTO cursor_rows VALUES (1),(2),(3),(4),(5);
  1. 使用 Connector/J URL 参数:
useServerPrepStmts=true&useCursorFetch=true&defaultFetchSize=2
  1. 执行:
try (PreparedStatement ps = conn.prepareStatement(
        "select id from cursor_rows order by id")) {
    ps.setFetchSize(2);
    try (ResultSet rs = ps.executeQuery()) {
        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
    }
}

Actual behavior

executeQuery() 直接失败,无法获得 ResultSet:

invalid input: unsupported Prepare flag '1'

Expected behavior

MatrixOne 应接受 Connector/J 发出的只读游标执行标志,并通过 MySQL server-side cursor / COM_STMT_FETCH 分批返回 5 行结果。

Stability and controls

  • Connector/J 8.0.15 reproducer: 3/3 失败,错误完全一致
  • Connector/J 8.0.33 reproducer: 3/3 失败,错误完全一致
  • 8.0.15 control (useCursorFetch=false): 3/3 通过,每次返回 5 行
  • 8.0.33 control (useCursorFetch=false): 3/3 通过,每次返回 5 行
  • 每次失败后新连接仍能完成对照查询,无数据丢失或残留事务
  • 测试库删除成功;服务进程仍在运行,未观察到 panic、hang 或重启

Evidence

Connector/J 8.0.15:

CURSOR_RUN_1=FAIL sqlState=HY000 code=20301 message=invalid input: unsupported Prepare flag '1'
CURSOR_RUN_2=FAIL sqlState=HY000 code=20301 message=invalid input: unsupported Prepare flag '1'
CURSOR_RUN_3=FAIL sqlState=HY000 code=20301 message=invalid input: unsupported Prepare flag '1'
CONTROL_RUN_1=PASS rows=5
CONTROL_RUN_2=PASS rows=5
CONTROL_RUN_3=PASS rows=5
CLEANUP=PASS
SUMMARY=cursorFailures:3/3,controls:3/3

Connector/J 8.0.33 得到相同的 3/3 失败和 3/3 对照通过结果。

Code analysis

  • pkg/frontend/mysql_protocol.go 的 parseStmtExecute 在读取 COM_STMT_EXECUTE flag 后,仅允许 flag == 0;flag != 0 直接返回 unsupported Prepare flag。代码中的 TODO 也注明目前只支持 CURSOR_TYPE_NO_CURSOR。
  • pkg/frontend/mysql_protocol_predefines.go 虽声明了 COM_STMT_FETCH (0x1c),但命令执行分支没有对应处理路径。
  • 因此根因是 MatrixOne MySQL binary protocol 尚未实现 Connector/J server-side cursor 所需的 CURSOR_TYPE_READ_ONLY 和后续 fetch 流程。

Regression coverage

建议修复时增加 motr/协议场景测试:使用 Connector/J 开启 useServerPrepStmts=true&useCursorFetch=true,设置正数 fetchSize,验证多批次读取、空结果、少于/等于/大于 fetch size、提前关闭 ResultSet,以及失败后的 statement/connection 可继续使用。

Related

Activity

  1. added this to the MO-202608 milestone on Aug 18, 2026
  2. added
    kind/bug
    on Aug 18, 2026
  3. added theissue type on Aug 18, 2026
  4. Ariznawlll commented on Aug 24, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:ba8954d543af0bdfb1948a8b89f77f5235ac9259
    • 部署方式:隔离本地单 LOG/TN/CN
    • 客户端:MySQL Connector/J 8.0.33
    • 测试日期:2026-08-24

    原问题验证

    开启 useServerPrepStmts=true、useCursorFetch=true 和 defaultFetchSize=2,以 server prepared query 读取 5 行。连续 3 轮均成功返回 [1,2,3,4,5],不再出现 Issue 中的 Prepare flag 拒绝。

    Happy Path 覆盖

    1. fetchSize=2 跨多页读取 5 行。
    2. 在同一个 PreparedStatement 上重复执行,结果保持 [1,2,3,4,5]。
    3. 空结果查询正确返回空集合。

    Unhappy Path 覆盖

    不适用:该问题的公开契约是只读游标执行。边界覆盖包含空结果和同一 statement 的重复读取;测试不修改数据,执行后表状态不变。

    白盒验证

    不适用:已通过真实 Connector/J 的 server cursor 协议路径完成验证。

    回归测试

    测试证据

    • 每轮均验证分页结果、复用与空结果。
    • 回归提交:a6a562a1。

    测试结论

    测试通过。

  5. daviszhen commented on Oct 16, 2026

    @daviszhen
    Contributor

    v4.2.5 release-specific reproduction (arm64)

    Independent replay on the official matrixorigin/matrixone:latest image — arm64/linux, VERSION() = 8.0.30-MatrixOne-v4.2.5, single-node standalone, Apple M5 host, mysql 8.4 CLI. This comment does not assert anything about current main; it only records that the behaviour is still present in this release image.

    Still reproducible. Connector/J 8.0.33 against v4.2.5:

    jdbc:mysql://<host>:16001/db?useCursorFetch=true&defaultFetchSize=100
    statement.setFetchSize(100);
    -- MO v4.2.5: SQLException: invalid input: unsupported Prepare flag '1'
    -- MySQL 8.0.46: reads the result set normally
    

    Because the failure happens when the statement is prepared, it affects any ORM/pool configuration that enables cursor fetching globally, rather than only large result sets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions