本文介绍 MySQL 8.x 驱动报错 ArrayIndexOutOfBoundsException。
适用版本
OceanBase 数据库 V2.2.x、V3.1.x、V3.2.x。
问题现象
使用 MySQL JDBC 8.0.26+ 驱动报错如下。
java.lang.SQLException: 0] with root cause
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at com.mysql.cj.protocol.a.NativePacketPayload.readInteger(NativePacketPayload.java:398)
at com.mysql.cj.protocol.a.NativePacketPayload.readString(NativePacketPayload.java:605)
at com.mysql.cj.protocol.a.NativeServerSessionStateController$NativeServerSessionStateChanges.init(NativeServerSessionStateController.java:112)
at com.mysql.cj.protocol.a.result.OkPacket.parse(OkPacket.java:66)
at com.mysql.cj.protocol.a.NativeProtocol.readServerStatusForResultSets(NativeProtocol.java:1755)
问题原因
根据 MySQL 文档,对于 MySQL JDBC 8.0.26+ 驱动,需要增加 JDBC url 参数 trackSessionState=true。
For Connection/J 8.0.26 and later:
Connector/J can receive information on client session state changes tracked by the server if the tracking has been enabled on the server.
The reception of the information is enabled by
setting the Connector/J connection property trackSessionState to true
(default value is false for the property).
解决方法
方法一 (推荐): 使用 MySQL JDBC 8.0.25 版本的驱动。
方法二: 对于 MySQL JDBC 8.0.26+ 驱动,需要增加 JDBC url 参数 trackSessionState=true。