Bug #29852 ClassCastException when closing connection
Submitted: 17 Jul 2007 18:08 Modified: 31 Aug 2007 14:20
Reporter: Luc Pezet Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.0.6 OS:Any
Assigned to: CPU Architecture:Any
Tags: ClassCastException, close, Connection

[17 Jul 2007 18:08] Luc Pezet
Description:
Throws an exception when closing connections (I think):

Caused by: java.lang.ClassCastException: java.util.HashMap$Entry
        at com.mysql.jdbc.LoadBalancingConnectionProxy.invoke(LoadBalancingConne
ctionProxy.java:325)
        at $Proxy3.close(Unknown Source)
...

How to repeat:
public void testMySQL() throws Exception {
    	Class.forName("com.mysql.jdbc.Driver");
    	Connection oCon = null;
    	try {
    	oCon = DriverManager.getConnection("jdbc:mysql:loadbalance://host1,host2/dbname", "user", "password");
    	Statement oStmnt = oCon.createStatement();
    	ResultSet oRSet = oStmnt.executeQuery("SELECT * FROM tests LIMIT 10");
    	while (oRSet.next()) {
    		System.out.println("#" + oRSet.getString(1));
    	}
    	oRSet.close();
    	oStmnt.close();
    	} catch (Exception e) {
    		e.printStackTrace();
    	} finally {
    		if (oCon != null) oCon.close();
    	}
}

Suggested fix:
Looking at the source in com/mysql/jdbc/LoadBalancingConnectionProxy (around line 325):
if ("close".equals(methodName)) {
	synchronized (this.liveConnections) {
	 // close all underlying connections
	 Iterator allConnections = this.liveConnections.entrySet()
			.iterator();
	 while (allConnections.hasNext()) {
		((Connection) allConnections.next()).close();
	 }
	 this.liveConnections.clear();
	 this.connectionsToHostsMap.clear();
        }
	return null;
}

"entrySet().iterator()" return an Iterator on HashMap.Entry.
So 
((Connection) allConnections.next()).close();
should be:
((Connection) allConnections.next().getValue()).close();
[17 Jul 2007 19:28] Luc Pezet
Suggested Fix:
It should be actually:
((Connection) ((Entry) allConnections.next()).getValue()).close();
[30 Jul 2007 13:37] Tonci Grgin
Hi Luc and thanks for your report. I can't repeat the problem with test case attached (it was written to comply with our test suite). Can you please try with latest snapshot and post the results?
[30 Jul 2007 13:38] Tonci Grgin
Test case

Attachment: TestIssue11987.java (text/x-java), 5.94 KiB.

[17 Aug 2007 7:49] Ian Hoogeboom
I'm having the same problem with Connector/J version 5.0.7.

java.lang.ClassCastException: java.util.HashMap$Entry
        at com.mysql.jdbc.LoadBalancingConnectionProxy.invoke(LoadBalancingConnectionProxy.java:325)
        at $Proxy58.close(Unknown Source)

I'm using JBoss 4.0.4.GA, no application deployed.
The message is appearing when JBoss is trying to destroy managed connections.
[30 Aug 2007 21:28] 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/33479
[30 Aug 2007 21:43] 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/33480
[30 Aug 2007 21:50] Mark Matthews
Will be released w/ 5.0.8.
[30 Aug 2007 21:51] 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/33481
[31 Aug 2007 14:20] MC Brown
A note has been added to the 5.0.8 changelog: 

Closing a load-balanced connection would cause a ClassCastException.
[6 Sep 2007 15:14] 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/33832
[7 Sep 2007 14:04] 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/33909
[3 Oct 2007 16:41] 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
[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
[11 Oct 2007 20:10] 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:51] 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