Bug #81706 NullPointerException in driver
Submitted: 3 Jun 2016 9:22 Modified: 8 Feb 2017 19:56
Reporter: Jurgen Voorneveld Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.1.39 to 5.1.30 OS:Any
Assigned to: CPU Architecture:Any
Tags: cacheResultSetMetadata, NullPointerException, ping, PreparedStatement

[3 Jun 2016 9:22] Jurgen Voorneveld
Description:
Version 5.1.38 of Connector/J driver dies with a NullPointerException when sending a ping request using a PreparedStatement while having cacheResultSetMetadata set to true. This bug works with both mysql and mariadb, it also works with older versions of the driver. The bug does not appear when any of the settings are changed or the newer version 6 driver is used.

The stacktrace:
$ java -jar target/test-1.0-SNAPSHOT.jar 
Exception in thread "main" java.lang.NullPointerException
        at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:70)
        at java.nio.ByteBuffer.wrap(ByteBuffer.java:373)
        at com.mysql.jdbc.StringUtils.toString(StringUtils.java:2195)
        at com.mysql.jdbc.Field.getStringFromBytes(Field.java:635)
        at com.mysql.jdbc.Field.getTableName(Field.java:662)
        at com.mysql.jdbc.Field.getFullName(Field.java:503)
        at com.mysql.jdbc.ResultSetImpl.buildIndexMapping(ResultSetImpl.java:699)
        at com.mysql.jdbc.ConnectionImpl.initializeResultsMetadataFromCache(ConnectionImpl.java:5283)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1972)
        at nl.ngti.mysql.driver.test.Test.main(Test.java:23)

context:
This bug appeared because in our setup we use the ping query as the standard connection validation query. "SELECT 1" is another common validation query which does not have this problem. The ping query is faster though.

How to repeat:
SSCCE: https://www.dropbox.com/s/romclnjyrhvtjw9/MySQLDriverTest.tar.gz?dl=0

Have a version of MySQL running on localhost (or some other place that the DB_HOST environment variable points to)

$ mvn clean package
$ java -jar target/test-1.0-SNAPSHOT.jar

or with docker:

$ mvn clean package
$ docker-compose build
$ docker-compose up

The docker compose file contains both mariadb and mysql images which you can select using the DB_HOST environment variable.

Suggested fix:
These are workarounds:
- Turn of result set metadata caching
- Use "SELECT 1" instead of ping for database connectivity check
- Use the newer version 6 driver
[3 Jun 2016 9:28] Jurgen Voorneveld
fixing the tags
[3 Jun 2016 10:39] Jurgen Voorneveld
Narrowed down the version number. Version 5.1.30 is the earliest that exhibits this bug.
[6 Jun 2016 13:09] Chiranjeevi Battula
Hello Jurgen Voorneveld,

Thank you for the bug report and test case.
Verified this behavior on MySQL Connector / J 5.1.30 to 5.1.39.

Thanks,
Chiranjeevi.
[6 Jun 2016 13:10] Chiranjeevi Battula
run:
Exception in thread "main" java.lang.NullPointerException
	at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:70)
	at java.nio.ByteBuffer.wrap(ByteBuffer.java:373)
	at com.mysql.jdbc.StringUtils.toString(StringUtils.java:2195)
	at com.mysql.jdbc.Field.getStringFromBytes(Field.java:635)
	at com.mysql.jdbc.Field.getTableName(Field.java:662)
	at com.mysql.jdbc.Field.getFullName(Field.java:503)
	at com.mysql.jdbc.ResultSetImpl.buildIndexMapping(ResultSetImpl.java:699)
	at com.mysql.jdbc.ConnectionImpl.initializeResultsMetadataFromCache(ConnectionImpl.java:5283)
	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1972)
	at javaapplication3.bug_81706.main(bug_81706.java:33)
C:\Users\cbattula\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
[8 Feb 2017 19:56] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 5.1.41 changelog:

"When the configuratino property cacheResultSetMetadata was set to true, a ping query using a PreparedStatement failed with a NullPointerException. This fix moves the ping query to an earlier stage of the statement execution, which prevents the exception."