Bug #36784 Connector/J doesn't set exception cause properly
Submitted: 18 May 2008 13:04 Modified: 18 May 2008 13:37
Reporter: Oded Arbel Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.0.8 OS:Any
Assigned to: CPU Architecture:Any
Tags: exception, java

[18 May 2008 13:04] Oded Arbel
Description:
When a com.mysql.jdbc.CommunicationsException exception is thrown, for example due to the connection being broken, the exception cause is not set up properly so its impossible to know the cause of the underlying disruption without text analysis of the exception message (a bad practice that should be avoided).

How to repeat:
For example, consider the following code:
try {
  doWork(from, to);
} catch (SQLException e) {
  if (e.getCause() instanceof java.io.EOFException) {
    handleConnectionDisruption(e);
    return;
  }
  s_logger.fatal("DB Exception '" + e + "' with cause " + e.getCause());
  throw e;
}

Now when doWork() breaks due to communication failure, we get this in the logs:
---8<----
DB Exception 'com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.io.EOFException
MESSAGE: Can not read response from server. Expected to read 70 bytes, read 6 bytes before connection was unexpectedly lost.

STACKTRACE:

java.io.EOFException: Can not read response from server. Expected to read 70 bytes, read 6 bytes before connection was unexpectedly lost.
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2464)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
        at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1360)
        at com.mysql.jdbc.RowDataDynamic.nextRecord(RowDataDynamic.java:366)
        at com.mysql.jdbc.RowDataDynamic.next(RowDataDynamic.java:355)
        at com.mysql.jdbc.ResultSet.next(ResultSet.java:7302)
        at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:168)
        at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:168)
        at ********************** (****.java:***)

** END NESTED EXCEPTION **

Last packet sent to the server was 206617 ms ago.' with cause null

Suggested fix:
com.mysql.jdbc.CommunicationsException should set the cause properly using the nested exception information which is apparently there as it reports it in the message.
[18 May 2008 13:37] Mark Matthews
Connector/J 5.1 calls initCause() for communications exceptions. 5.1 is the current production/ga release of the JDBC driver. Please upgrade to 5.1.x to get this behavior.
[2 Oct 2008 13:50] 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/55081

2828 Matthias Leich	2008-10-02 [merge]
      Merge 5.1 -> 6.0 of fix for Bug#36784
      main.slow_launch_time_func test fails randomly
[17 Oct 2008 16:43] Bugs System
Pushed into 6.0.8-alpha  (revid:matthias.leich@sun.com-20081002134941-ianl8ovmet95dhs4) (version source revid:matthias.leich@sun.com-20081004164917-ly18y5m21rz1l2dz) (pib:5)