Bug #10630 NullPointerException : com.mysql.jdbc.Statement.getWarnings(Statement.java:704)
Submitted: 13 May 2005 17:47 Modified: 17 Oct 2005 19:42
Reporter: Robert Simpson Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:MySQL Connector/J 3.1.8 OS:Windows (Windows XP)
Assigned to: Mark Matthews CPU Architecture:Any

[13 May 2005 17:47] Robert Simpson
Description:
This should probably be permanently documented, maybe in the FAQs on the web site....

After upgrading to MySQL Connector/J version 3.1.8, a few intermittent NullPointerExceptions were being experienced in web-based (Tomcat) SQLj applications.  The stack trace produced is:

   java.lang.NullPointerException
           at com.mysql.jdbc.Statement.getWarnings(Statement.java:704)
           at sqlj.runtime.profile.ref.RTStatementJDBCBase.getWarnings(RTStatementJDBCBase.java:500)
           at sqlj.runtime.ExecutionContext$StatementFrame.releaseStatement(ExecutionContext.java:1320)
           at sqlj.runtime.ExecutionContext.releaseStatement(ExecutionContext.java:988)

The reason for this exception that, if the SQLj iterator is not properly closed as soon as it is no longer needed, SQLj will automatically close the interator when the statement is benig released prior to being resused.  If some time has passed since the statement was originally executed, the MySQL connection object in the Statement class may be null, resulting in a NullPointerException when it tries to check the version of the database in the connection.

How to repeat:
In a web-based SQLj application, remove the code that closes the iterator.  Run the application once from a browser and wait for awhile.  Then try running it again.  If it still works, wait longer next time.

Suggested fix:
In the application code, make sure the SQLj iterators are closed as soon as they are no longer needed.  A possible change to MySQL Connector/J would be to throw an SQLException indicating that the connection is no longer available, rather than a NullPointerException which initially looks like it might be a bug in the connector.
[13 Jul 2005 19:43] Robert Simpson
Looks like this might be more of a bug than I originally thought.  We have also been experiencing this problem with "select ... into ..." statements in SQLj, and since in that case there's no iterator to close I have been unable to find a workaround.  The problem is that SQLj does not reset the statement until the next time the same statement is used, and by that time MySQL may have released the connection.

Suggested fix:  Make sure the connection is not null before calling "getWarnings" on the connection object:

...
if (this.connection != null && this.connection.getIO().versionMeetsMinimum(4, 1, 0)) {
...