Bug #27412 NullPointerException at Connection.initializeResultsMetadataFromCache
Submitted: 24 Mar 2007 0:30 Modified: 8 Oct 2007 15:40
Reporter: Gökhan Demir Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.0.5 OS:Linux (rhel4)
Assigned to: CPU Architecture:Any

[24 Mar 2007 0:30] Gökhan Demir
Description:
I get the following stacktrace when using 5.0.5:
java.lang.NullPointerException:
        at com.mysql.jdbc.Connection.initializeResultsMetadataFromCache(Connection.java:5882)
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:853)
...

when connection cache properties are:
cachePrepStmts=true
useServerPrepStmts=false
cacheResultSetMetadata=true

The error does not happen in 5.0.4

How to repeat:
/* just execute a prepared statement */
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement( "select 1" );
stmt.execute();

Suggested fix:
set cacheResultSetMetadata=false in your connection cache.
[24 Mar 2007 0:34] Gökhan Demir
I forgot to mention that, I have tested this against a 5.0.37 on rhel4 and against a 5.0.27 on fedora core 2. Outcome is the same.
[24 Mar 2007 17:24] Tonci Grgin
Hi and thanks for your report. It is almost impossible to tell what went wrong just by looking at the stack trace.
Please attach complete test case producing this error every time. What id your JDK/JRE version?
[27 Mar 2007 13:40] Gökhan Demir
A very simple test case to produce the mentioned error.

Attachment: TestMysqlJdbc505Bug.java (application/octet-stream, text), 1.74 KiB.

[12 Apr 2007 7:08] Tonci Grgin
Hi Demir. Verified as described with test case attached using latest SVN sources.

We have two problems here, first one (ExecuteQuery vs Execute) you have found. The thing is that Execute tries to figure out wether or not statement returns resultset; obviously it guessed wrong as resultSet is NULL thus raising exception:
java.lang.NullPointerException
	at com.mysql.jdbc.Connection.initializeResultsMetadataFromCache(Connection.java:5891)
cachedMetaData.fields = resultSet.fields;

This is due to problem 2. Putting props.put("useServerPrepStmts", "false"); you made driver guess about "preparability" of statement, and, in this case, driver correctly concludes that preparing this statement is waste of time:
		     10 Connect     root@localhost on test
		     10 Query       SHOW VARIABLES
070412  8:38:03	     10 Query       SHOW COLLATION
070412  8:38:04	     10 Query       SET character_set_results = NULL
		     10 Query       SET autocommit=1
		     10 Query       SET sql_mode='STRICT_TRANS_TABLES'
070412  8:38:31	     10 Query       SELECT 1
070412  8:41:38	     10 Quit       

But, by putting props.put("useServerPrepStmts", "true");, you have another situation:
		     12 Connect     root@localhost on test
		     12 Query       SHOW VARIABLES
		     12 Query       SHOW COLLATION
		     12 Query       SET character_set_results = NULL
		     12 Query       SET autocommit=1
		     12 Query       SET sql_mode='STRICT_TRANS_TABLES'
		     12 Prepare     [1] SELECT 1
		     12 Execute     [1] SELECT 1
		     12 Quit       
and there's no error thrown:
Connected to 5.0.38-log
java.vm.version         : 1.5.0_11-b03
java.vm.vendor          : Sun Microsystems Inc.
java.runtime.version    : 1.5.0_11-b03
os.name                 : Windows XP
os.version              : null
sun.management.compiler : HotSpot Client Compiler

Time: 0,485

OK (1 test)
[12 Apr 2007 7:09] Tonci Grgin
Test case

Attachment: TestBug27412.java (text/x-java), 3.92 KiB.

[2 Oct 2007 19:36] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34781
[2 Oct 2007 19:49] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34784
[2 Oct 2007 19:55] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34785
[3 Oct 2007 16:42] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34839
[3 Oct 2007 16:46] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34840
[3 Oct 2007 18:59] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/34855
[4 Oct 2007 20:41] Mark Matthews
Fixed for 5.0.8.
[5 Oct 2007 18:53] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35011
[8 Oct 2007 15:40] MC Brown
A note has been added to the 5.0.8 changelog: 

Cached metadata with PreparedStatement.execute() throws NullPointerException.
[11 Oct 2007 20:11] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35407
[11 Oct 2007 20:24] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35410
[11 Oct 2007 20:52] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/35414
[19 Nov 2007 0:57] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/38019
[19 Nov 2007 2:52] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/38024