1

I have a Java web server. When I access it after it is idle for the night, I get the following exception: "statement has been closed, no further internal information available".

In order to debug this, I added a check in the code:

        try 
        {
            if (stmt.isClosed())
                throw new Exception("Aha!");
            stmt.setString(1, lemma);
            rs = stmt.executeQuery();   
        } 
        catch (SQLException e)  {
            e.printStackTrace();
        }

But, I don't see the exception 'Aha!', I see this exception:

statement has been closed, no further internal information available
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 82,918,766 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3056)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2942)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3485)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2264)
    at eu.excitementproject.eop.core.component.lexicalknowledge.similarity.AbstractSimilarityLexicalResource.getRulesForSide(AbstractSimilarityLexicalResource.java:122)
    ... 69 more
Caused by: java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:114)
    at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:161)
    at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:189)
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2500)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2953)
    ... 77 more

What does it mean, and how can I prevent this?

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • 1
    http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – AllTooSir May 01 '13 at 04:52

1 Answers1

2

It may happen that the connection closes but leaves the statement open. In this case, a call to Statement.isClosed() will return false, but exception will still be thrown.

Try

if (stmt.isClosed() || stmt.getConnection().isClosed())
    throw new Exception("Aha!");
Felype
  • 3,087
  • 2
  • 25
  • 36