Bug #38699 IllegalArgumentException thrown when invoking close
Submitted: 10 Aug 2008 15:22 Modified: 1 Sep 2008 18:09
Reporter: Chris Webster Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version:5.1.6 OS:Any
Assigned to: CPU Architecture:Any

[10 Aug 2008 15:22] Chris Webster
Description:
An IllegalArgmentException is thrown from com.mysql.jdbc.jdbc2.optional.JDBC4PreparedStatementWrapper.close (line 70) when this method is invoked because this.pooledConnection has been set to null in the super.close() (line 67) invocation. 

This presents a serious problem as this is an unexpected RuntimeException. 

How to repeat:
Invoke the close method on com.mysql.jdbc.jdbc2.optional.JDBC4PreparedStatementWrapper.close(JDBC4PreparedStatementWrapper.java:70)

Suggested fix:
The way to resolve this without changing the current design (relying on an instance of StatementWrapper), would be to change the JDBC4PreparedStatementWrapper.close method to be something like:

public void close() throws SQLException {
    JDBC4MysqlPooledConnection currentConnection = 			(JDBC4MysqlPooledConnection) this.pooledConnection;
     try {
	super.close();
     } finally {
	try {
	    currentConnection.fireStatementEvent(
		new StatementEvent(currentConnection, this));
	} finally {
	    this.unwrappedInterfaces = null;
	}
     }
}

This approach caches the pooledConnection so when it is nullified in StatementWrapper the local reference is maintained. Another possible approach would be to move the event firing to StatementWrapper (perhaps in a method with a boolean parameter which specified whether to fire an event or not). This approach would provide better encapsulation.
[10 Aug 2008 15:24] Chris Webster
relevant portion of the stack trace

Attachment: stacktrace.txt (text/plain), 2.71 KiB.

[12 Aug 2008 10:04] Tonci Grgin
Hi Chris and thanks for your report.

Can you please attach small but complete test case proving your point and add some more info on JDK/other SW used.
[12 Aug 2008 18:40] Nam Nguyen
Hi Tonci.  We don't have a direct test case.  The situation is that we were using GlassFish connection pool with datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource" and res-type="javax.sql.DataSource".  If we use res-type="javax.sql.ConnectionPoolDataSource" then we hit the exception stack.  Wouldn't the exception stack be enough info to fix the code?
[13 Aug 2008 16:45] Nam Nguyen
Simple test case to reproduce the "null source" exception.

Attachment: Main.java (application/octet-stream, text), 988 bytes.

[13 Aug 2008 16:54] Nam Nguyen
Hi Tonci, I attached a simple test case.  I am using JDK 6.
[15 Aug 2008 0:45] Chris Webster
test case provided
[18 Aug 2008 19:48] Mark Matthews
This should be fixed in the head of 5.1, as the fix was needed when we fixed Bug#35489. Have you tried a nightly snapshot build of 5.1? It should have the fix for this. See http://downloads.mysql.com/snapshots.php
[30 Aug 2008 19:50] Tonci Grgin
Thanks Mark. Anyone tried latest code?
[31 Aug 2008 15:32] Nam Nguyen
The snapshots page does not have any links to download.  It seems that the build is broken.
[1 Sep 2008 5:48] Sveta Smirnova
Nam,

please scroll down the page or use this link: http://downloads.mysql.com/snapshots/mysql-connector-java-5.1/mysql-connector-java-5.1-nig... or http://downloads.mysql.com/snapshots/mysql-connector-java-5.1/mysql-connector-java-5.1-nig...
[1 Sep 2008 17:45] Nam Nguyen
Verified that the problem is fixed with mysql-connector-java-5.1-nightly-20080901-bin.jar
[1 Sep 2008 18:09] Tonci Grgin
Thanks Nam, closing the report now as it is fixed in source trees.
[5 Sep 2008 19:41] Sveta Smirnova
Bug #37645 was marked as duplicate of this one.