Bug #35489 JDBC4PreparedStatementWrapper: NPE on close()
Submitted: 21 Mar 2008 19:57 Modified: 25 Jul 2008 13:17
Reporter: Jens Elkner Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version:5.1.6 OS:Any
Assigned to: Mark Matthews CPU Architecture:Any

[21 Mar 2008 19:57] Jens Elkner
Description:
The JDBC4PreparedStatementWrapper.close() calls StatementWrapper.close() which in turn sets the instance's  pooledConnection to 'null'. After that, it tries to fire a StatementEvent using this.pooledConnection, which has been already set to 'null'. So one always gets an NPE if one tries to close a JDBC4 enhanced prepared statement.

How to repeat:
try to close an successfully executed pstmt.

Suggested fix:
--- mysql-connector-java-5.1.6/src/com/mysql/jdbc/jdbc2/optional/JDBC4PreparedStatementWrapper.java     Wed Mar  5 17:27:00 2008
+++ ../mysql-connector-java-5.1.6/src/com/mysql/jdbc/jdbc2/optional/JDBC4PreparedStatementWrapper.java  Fri Mar 21 20:06:25 2008
@@ -63,12 +63,13 @@
        }

        public void close() throws SQLException {
+               JDBC4MysqlPooledConnection con =
+                       (JDBC4MysqlPooledConnection) this.pooledConnection;
                try {
                        super.close();
                } finally {
                        try {
-                               ((JDBC4MysqlPooledConnection)this.pooledConnection).fireStatementEvent(
-                                               new StatementEvent(this.pooledConnection, this));
+                               con.fireStatementEvent(new StatementEvent(con, this));
                        } finally {
                                this.unwrappedInterfaces = null;
                        }
[14 Apr 2008 22:53] Ed Hager
I ran into the same problem trying to use com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource with Hibernate and Resin:

Caused by: java.lang.IllegalArgumentException: null source
at java.util.EventObject.<init>(EventObject.java:38)
at javax.sql.StatementEvent.<init>(StatementEvent.java:39)
at com.mysql.jdbc.jdbc2.optional.JDBC4PreparedStatementWrapper.close(JDBC4PreparedStatementWrapper.java:70)
at com.caucho.sql.UserStatement.close(UserStatement.java:127)
at com.caucho.sql.UserPreparedStatement.close(UserPreparedStatement.java:450)
at org.hibernate.jdbc.AbstractBatcher.closePreparedStatement(AbstractBatcher.java:534)
[15 Apr 2008 21:00] Jens Elkner
NOTE: http://iws.cs.uni-magdeburg.de/~elkner/mysql/java/5.1.6-jdbc4-stmt-close-NPE.patch

got an update, since the initial patch took care of PooledConnection, only. The update fixes XA and SuspendableXAConnections as well.
[16 Jul 2008 14: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/49848
[25 Jul 2008 13:17] Tony Bedford
An entry has been added to the 5.1.6 changelog:

Prepared statements from pooled connections caused a NullPointerException when closed() under JDBC-4.0.
[30 Jul 2008 14: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/50724