Handling of Binding Variable Values as Null Causes NullPointerException

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 绑定变量值为null的处理,引发NullPointerException

| username: TiDBer_YA0HosQn

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?

| username: buddyyuan | Original post link

Try replacing MySQL driver 8.0.22 with MySQL driver 5.

| username: TiDBer_YA0HosQn | Original post link

Rolling back the driver is not feasible; version 5 is too old. Currently, we have set spring.jdbc.getParameterType.ignore=true to skip the sqlTypeToUse step, but we still hope TiDB can look into whether there is a way to handle it in a manner compatible with MySQL.

| username: ShawnYan | Original post link

Hello, could you please specify the TiDB version?
select tidb_version()\G

| username: TiDBer_YA0HosQn | Original post link

Release Version: v5.0.1
Edition: Community
Git Commit Hash: 1145e347d3469d8e89f88dce86f6926ca44b3cd8
Git Branch: heads/refs/tags/v5.0.1
UTC Build Time: 2021-04-23 05:51:17
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false

| username: Min_Chen | Original post link

It is recommended to use JDBC 5.1.49, as the official JDBC 8 testing is not very comprehensive.

| username: TiDBer_rrt4FOef | Original post link

Encountered the same issue on TiDB 6.1.2, using the driver com.mysql:mysql-connector-j:8.0.31

java.lang.NullPointerException: null
	at com.mysql.cj.jdbc.result.ResultSetMetaData.getColumnType(ResultSetMetaData.java:190)

Debugging revealed that the Filed array obtained in ResultSetMetaData has a length equal to the actual number of fields, but the last Filed is null, causing a NullPointerException.

| username: 会飞的土拨鼠 | Original post link

When using mysql-connector-j-8.0.31.jar, some prompts may appear. You can use a similar JDBC driver like mysql-connector-java-5.1.47-bin.jar. In the Spring Boot yml configuration file, there are configurations for com.mysql.cj.jdbc.Driver (MySQL 8.0) and com.mysql.jdbc.Driver (MySQL 5.7).

| username: 会飞的土拨鼠 | Original post link

The database version used by TiDB corresponds to MySQL 5.7.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.