Bug #64805 StatementImpl$CancelTask occasionally throws NullPointerExceptions
Submitted: 29 Mar 2012 18:07 Modified: 22 Feb 2013 0:18
Reporter: Adam Bradley Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.18 OS:Any
Assigned to: Alexander Soklakov CPU Architecture:Any

[29 Mar 2012 18:07] Adam Bradley
Description:
StatementImpl.java:108 references "connection.getQueryTimeoutKillsConnection()".  However, it's possible for "connection" to have been set to "null" by the time this executes.  This causes short NullPointerException stack traces to be printed to the terminal.

How to repeat:
Caused by a race between closing the statement and the query timeout firing, so difficult to intentionally reproduce.

Suggested fix:
Refactor the NullPointerException catches inside of each of the if-branches into a single block that wraps the whole if statement.
[10 Apr 2012 7:13] Alexander Soklakov
Adam, could you refine, do you share Statement between threads?
[10 Apr 2012 18:46] Adam Bradley
Not at the level of our application code.  We are using c3p0, I don't know if it shares references to Statements across threads.
[3 Jul 2012 19:49] Scott E
I can reproduce this as well, using a case that looks something along the lines of:

JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.setQueryTimeout(2);
jdbc.execute("select sleep(2);");

I haven't been able to nail down the exact set of circumstances how it occurs though.

mysql connector 5.1.20
spring 3.0.7
[22 Jan 2013 9:03] Alexander Soklakov
Hi Adam,

It looks reasonable to fix as you proposed.
[22 Jan 2013 9:05] Alexander Soklakov
Scott, thanks for repeatable test case!
[22 Feb 2013 0:18] John Russell
Added to changelog for 5.1.24: 

The connection.getQueryTimeoutKillsConnection() method could be
called with a null parameter, causing NullPointerException errors and
stack traces. The error depended on a race condition and so occurred
intermittently.