Bug #21480 Incorrect exception thrown when using timeout
Submitted: 7 Aug 2006 12:49 Modified: 22 Feb 2007 11:23
Reporter: Erik Ekman Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:All OS:Any (All)
Assigned to: Mark Matthews CPU Architecture:Any

[7 Aug 2006 12:49] Erik Ekman
Description:
Different stack traces are received when connecting fails normally versus when using connectTimeout.

Normal:
java.net.SocketException: java.net.ConnectException: Connection timed out: connect
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2555)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
	at MysqlTest.main(MysqlTest.java:23)

With timeout:
java.net.SocketException: java.lang.reflect.InvocationTargetException
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:198)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
	at com.mysql.jdbc.Connection.createNewIO(Connection.java:2555)
	at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
	at MysqlTest.main(MysqlTest.java:16)

How to repeat:
Will attach example program.

Suggested fix:
Will attach patch. Simply check if the cause of the InvocationTargetException is a SocketException or SocketTimeoutException, and throw that one instead.
[7 Aug 2006 12:49] Erik Ekman
Test application

Attachment: MysqlTest.java (application/octet-stream, text), 888 bytes.

[7 Aug 2006 12:50] Erik Ekman
Suggested patch

Attachment: timeoutexception.patch (application/octet-stream, text), 804 bytes.

[8 Aug 2006 11:44] Erik Ekman
A similar fix should probably be applied to the case when not using timeout (around line 156) to avoid wrapping the exception in a SocketException. 

And SocketTimeoutException should be added at line 203 to avoid wrapping the SocketTimeoutException. Should I send a new patch?
[8 Aug 2006 14:13] Tonci Grgin
Erik, feel free to post your fix. Thanks for your interest in MySQL.

Regards
Tonci
[9 Aug 2006 7:50] Erik Ekman
New patch

Attachment: unwrap_exceptions.patch (application/octet-stream, text), 1.56 KiB.

[9 Aug 2006 14:19] Tonci Grgin
Verified as described by reporter with test case provided. I used mysql-connector-java-3.1.13 and mysql-connector-java-5.0-nightly-20060704.
[10 Jan 2007 21: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/17893
[10 Jan 2007 22:12] Mark Matthews
I fixed this a little bit differently than proposed by your patch to avoid copy-and-paste of code (centralized into one method), and to avoid newer JVM methods when unwrapping exceptions, since we still have users who can't move off of JDK-1.2 and 1.3.
[22 Feb 2007 11:23] MC Brown
A note has been added to the 5.0.5 changelog.