Skip to content

[Bug]: server prepared SELECT star keeps stale result metadata after ALTER ADD COLUMN #29180

Closed as not planned

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

57c280e

Other Environment Information

  • Hardware parameters: Apple Silicon local environment
  • OS type: macOS
  • Others: MatrixOne 1 LOG / 1 TN / 2 CN / Proxy; MySQL Connector/J 8.3.0; server-side prepared statements enabled

Actual Behavior

A server-side prepared SELECT * retains the result metadata captured when the table had two columns. After another connection executes ALTER TABLE ... ADD COLUMN, reusing the same PreparedStatement returns a three-column result description without refreshing the prepared column definition.

Connector/J then accesses the third field through a two-entry cached definition and raises:

java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
Caused by: java.lang.ArrayIndexOutOfBoundsException
  at com.mysql.cj.protocol.a.MergingColumnDefinitionFactory.createFromFields(...)
  at com.mysql.cj.jdbc.ServerPreparedStatement.executeInternal(...)

The behavior occurs when the DDL is executed on the same CN, another CN, and through a Proxy connection. A client-side prepared-statement control refreshes the result correctly.

Expected Behavior

Executing the existing server-side PreparedStatement after the table metadata changes should reprepare or otherwise return a column definition consistent with the result set. For the example below, the reused SELECT * should expose three columns and must not cause a Connector/J runtime exception.

Steps to Reproduce

String url = "jdbc:mysql://127.0.0.1:6001/db"
    + "?useServerPrepStmts=true&cachePrepStmts=true";

try (Connection query = DriverManager.getConnection(url, user, password);
     Connection ddl = DriverManager.getConnection(url, user, password);
     Statement change = ddl.createStatement();
     PreparedStatement ps = query.prepareStatement(
         "select * from jdbc_prepared_ddl where id=?")) {

  change.execute("create table jdbc_prepared_ddl(id int primary key, v varchar(32))");
  change.executeUpdate("insert into jdbc_prepared_ddl values(1,'initial')");

  ps.setInt(1, 1);
  try (ResultSet rs = ps.executeQuery()) {
    rs.next();
    assert rs.getMetaData().getColumnCount() == 2;
  }

  change.execute("alter table jdbc_prepared_ddl add column extra int default 7");

  ps.setInt(1, 1);
  try (ResultSet rs = ps.executeQuery()) { // Connector/J throws here
    rs.next();
    assert rs.getMetaData().getColumnCount() == 3;
  }
}

Additional information

  • Same-CN path: reproduced 3/3.
  • Cross-CN DDL invalidation path: reproduced 3/3.
  • Proxy query path with DDL on a CN: reproduced 3/3.
  • useServerPrepStmts=false control: the same lifecycle completed correctly 3/3, including add/drop column, view recreation, rename/rename-back, and table drop/recreate.
  • Related Java/JDBC release-gate tracking: [TestCase]: Java + MyBatis+MO framework scenario testing #26623.

Activity

  1. Ariznawlll commented on Sep 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充结果形状变更矩阵(同一固定提交 57c280ed48fdccec3c2eb25042820deaf1eca3c1):

    ArrayIndexOutOfBoundsException 不只发生在 ALTER TABLE ADD COLUMN。以下两种生命周期也会让既有的 server-side PreparedStatement 保留较短的旧列定义,随后接收更多的新列:

    1. SELECT * 在两列表上 prepare;随后 drop/recreate 同名表为三列;复用原 statement 时 Connector/J 报 Index 2 out of bounds for length 2。
    2. SELECT * 在两列 view 上 prepare;随后 drop/recreate 同名 view 为三列;复用原 statement 时出现相同异常。

    上述两种情况在以下路径各重复 3 次,现象一致:

    • 查询和 DDL 位于同一 CN;
    • 查询位于 CN1、DDL 位于 CN2;
    • 查询经 Proxy、DDL 位于 CN2。

    问题边界目前比较明确:

    • 结果形状扩张(2 列 → 3 列)存在旧 prepared 列定义与新结果不一致;
    • 结果形状缩减(3 列 → 2 列)、列重命名、INT → VARCHAR 类型修改、列顺序调整均能在原 server-side statement 上刷新为新元数据和新值;
    • useServerPrepStmts=false 下,add/drop column、rename、类型修改、列重排、表和 view 重建都返回正确的新列定义。

    因此当前证据指向 server prepared 结果元数据在“列数增加”时没有完成兼容的 reprepare/refresh,而不是通用 DDL 可见性问题。

  2. added this to the MO-202609 milestone on Sep 23, 2026
  3. added theissue type on Sep 23, 2026
  4. aptend commented on Sep 29, 2026

    @aptend
    Contributor

    I reproduced all three reported two-column → three-column cases—adding a column, recreating the table, and recreating the view—using Connector/J 8.3.0 against MySQL 8.4.11. All three produce the same ArrayIndexOutOfBoundsException; all three pass with useServerPrepStmts=false.

    MySQL bug #97924 records the same behavior. MySQL verified it and ultimately classified it as “Not a Bug”, explaining that Connector/J retains prepared result metadata across schema changes.

    This confirms the reported failure, but does not indicate a MatrixOne-specific defect. I suggest treating #29180 as a Connector/J compatibility limitation.

  5. assigned and unassigned on Sep 29, 2026
  6. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • MatrixOne 审核基线:main@d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 客户端对照:MySQL Connector/J 8.3.0;MySQL 8.4.11
    • 原始 MatrixOne 环境:1 Log / 1 TN / 2 CN / Proxy,server-side prepared statement
    • 文档合入:Ariznawlll/mo-test-reports@733aa7cdbec7c0043236d9d230933503efbb4a96
    • 测试日期:2026-10-02

    原问题验证

    研发已使用 Connector/J 8.3.0 对 MatrixOne 和 MySQL 8.4.11 进行相同对照:复用已执行过的 server-side prepared SELECT *,随后通过另一连接 ADD COLUMN、重建表或重建 view,三类场景都会因 Connector/J 保留旧结果元数据而触发 ArrayIndexOutOfBoundsException。MySQL 对应 Bug #97924 也被判定为 Not a Bug,因此该现象不是 MatrixOne 特有的服务端缺陷。

    Happy Path 覆盖

    • useServerPrepStmts=false 的 client-side prepared control 在上述 DDL 生命周期中可以重新读取列定义;
    • DDL 后关闭并重新 prepare statement、必要时重建连接,可以避免复用旧 metadata;
    • 未发生结果 schema 变化时,普通 prepared 查询不受本条限制影响。

    Unhappy Path 覆盖

    • 覆盖 ADD COLUMN、同名表 drop/recreate、同名 view drop/recreate;
    • MatrixOne same-CN、cross-CN 及 Proxy 路径均已复现;
    • 限制仅针对旧 server-side prepared handle 的 Connector/J metadata cache,不豁免服务端返回错误列定义、协议损坏、panic 或新 statement 仍返回旧 metadata 等独立问题。

    白盒验证

    本条没有 MatrixOne 修复 PR。研发结论指向 Connector/J 对 prepared result metadata 的缓存行为,并由 MySQL 8.4.11 对照确认。对应产品边界已写入兼容性文档 JDBC-001。

    回归测试

    该场景依赖特定 Connector/J 版本,并且“旧 handle 在 DDL 后抛异常”是已接受的客户端限制,不应作为预期失败固化到 MOTR;否则驱动未来修复后反而会造成回归误报。文档记录了推荐替代方案和仍需作为 bug 处理的边界。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

area/frontendkind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions