1

Is it supposedly possible for java.sql.Statement.execute to throw java.io.EOFException?

In http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#execute%28java.lang.String,%20int%29 it is said that it may throw only java.sql.SQLException. However, I got this exception message logged:

** BEGIN NESTED EXCEPTION **

java.io.EOFException

STACKTRACE:

java.io.EOFException
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1394)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1538)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1929)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
        at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1224)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2244)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2192)
        at com.mysql.jdbc.Statement.execute(Statement.java:906)
        at com.mysql.jdbc.Statement.execute(Statement.java:947)
        at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.logicalcobwebs.proxool.ProxyStatement.invoke(ProxyStatement.java:100)
        at org.logicalcobwebs.proxool.ProxyStatement.intercept(ProxyStatement.java:57)
        at $java.sql.Statement$$EnhancerByCGLIB$$4e952ca0.execute()
        at handler.do_insert(handler.java:734)

On line 733 of "handler.java" there is the "execute" call, and on 734 there is a call to getGeneratedKeys (which also may throw only SQLException).

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Jānis Elmeris
  • 1,975
  • 1
  • 25
  • 43
  • What may be happening to you is the connection getting closed on you. That's not within the scope of mySQL as it is, but rather a socket problem. – ATaylor Dec 13 '12 at 16:24
  • Is your code definitely catching the Exception, as in it's not just some error output from code deeper in the stack catching the Exception? – Omaha Dec 13 '12 at 16:25
  • Sorry, the code actually did catch the exception. I had got confused; probably because the stack trace (it is snipped in my question) went all the way up to the main program, so I got an impression that the exception was not caught. – Jānis Elmeris Dec 13 '12 at 20:58

2 Answers2

4

Looking at "BEGIN NESTED EXCEPTION" makes me think the EOFException is nested inside an SQLException - could this be the case?

The method can only throw an SQLException, but the SQLException exception could wrap another Exception (or Error)

Terry Horner
  • 497
  • 6
  • 16
  • The conditions for SQLException include "if a database access error occurs". I would expect the SQLException to wrap some form of IOException in that case. – Patricia Shanahan Dec 13 '12 at 16:31
2

Look for the words ** BEGIN NESTED EXCEPTION **. This exception is not thrown by execute(), it's a nested exception. That means that is was passed as the cause when creating the SQLException. Something like this:

try {

    ...

} catch (EOFException e) {
    throw new SQLException(e);
}
Pablo
  • 3,655
  • 2
  • 30
  • 44
  • Thank you for the explanation and an example of how the wrapping can be done! My mistake actually was misreading the log and thinking that the exception was not getting caught when it actually was (as SQLException). – Jānis Elmeris Dec 13 '12 at 21:01