Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 绑定变量值为null的处理,引发NullPointerException
To improve efficiency, please provide the following information for a quicker resolution:
【TiDB Usage Environment】Production, Testing, Research
【TiDB Version】TiDB 5.7.25
【Problem Encountered】Spring-JDBC 5.2.21 + MySQL driver 8.0.22 throws NullPointerException when handling bound variable values as null in this version.
【Reproduction Path】SQL: select * from any_table where instr(:a, ‘a’); – :a bound variable value is null
【Problem Phenomenon and Impact】Using Spring-JDBC 5.2.21 for TiDB data access, the org.springframework.jdbc.core.StatementCreatorUtils.setNull method fails to get the ResultSetMetaData object from MysqlParameterMetadata metadata, causing a JDBC getParameterType call failure and throwing a NullPointerException.
Tried the same combination, only replacing TiDB with MySQL 5.7, and it returned results normally.
The issue occurs at sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex); where an NPE is thrown. Earlier versions of Spring-JDBC caught the exception, but after upgrading to Spring-JDBC 5.2.21, only SQLException is caught, hence the NPE error.
if (tryGetParameterType) {
try {
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
}
catch (Throwable ex) {
if (logger.isDebugEnabled()) {
logger.debug("JDBC 3.0 getParameterType call not supported - using fallback method instead: " + ex);
}
}
}
Can TiDB support correctly returning the SQL type for null, similar to MySQL, by returning a sqlTypeToUse as null?