title: what is java.io.EOFException, Message: Can not read response from server. Expected to read 4 bytes, read 0 bytes
body: <p>This question has been asked a couple of times in SO and many times in other sites. But I didn't get any satisfiable answer.  </p>
  
  <p>My problem:<br>
  I have a java web application which uses simple <strong>JDBC</strong> to connect to <strong>mysql</strong> database through <strong>Glassfish</strong> application server.  </p>
  
  <p>I have used connection pooling in glassfish server with the following configurations:<br>
  Initial Pool Size: 25<br>
  Maximum Pool Size: 100<br>
  Pool Resize Quantity: 2<br>
  Idle Timeout: 300 seconds<br>
  Max Wait Time: 60,000 milliseconds  </p>
  
  <p>The application has been deployed for last 3 months and it was running flawlessly too.<br>
  But from last 2 days the following error is coming at the time of login.  </p>
  
  <p><strong>Partial StackTrace</strong>  </p>
  
  <pre><code>com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:  
  
  ** BEGIN NESTED EXCEPTION **  
  
  com.mysql.jdbc.CommunicationsException  
  MESSAGE: Communications link failure due to underlying exception:  
  
  ** BEGIN NESTED EXCEPTION **  
  
  java.io.EOFException  
  MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.  
  
  STACKTRACE:  
  
  java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.  
  at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)  
  at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2411)  
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)  
  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)  
  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)  
  at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)  
  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)  
  at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1448)  
  ............  
  ............  
  my application traces....  
  </code></pre>
  
  <p><strong>What caused this error suddenly ?</strong> I have lost a lot of time for this.</p>
  
  <p><strong>EDIT :</strong> The problem even persists after restarting the server. As per DBA two of the important mysql server configurations are:<br>
  <strong>wait_timeout :</strong> 1800 seconds<br>
  <strong>connect_timeout :</strong> 10 seconds<br>
  <strong>NOTE :</strong> Other applications deployed in the same server connecting to the same database and using different pools are running smoothly.  </p>
  
  <p><strong>EDIT-2 :</strong> After reading a lot of things and expecting some positive outcome I made these changes to my connection pool.  </p>
  
  <p><strong>Max Wait Time :</strong> 0 (previously it was 60 seconds)<br>
  <strong>Connection Validation :</strong> Required<br>
  <strong>Validation Method :</strong> table<br>
  <strong>Table Name :</strong> Demo<br>
  <strong>Validate Atmost Once :</strong> 40 seconds<br>
  <strong>Creation Retry Attempts :</strong> 1<br>
  <strong>Retry Intervals :</strong> 5 seconds<br>
  <strong>Max Connection Usage :</strong> 5  </p>
  
  <p>And this worked as the application is running for 3 days consistently. But I got a very strange and interesting result of out this. While monitoring the connection pool, I found these figures:  </p>
  
  <p><strong>NumConnAcquired :</strong> 44919 Count<br>
  <strong>NumConnReleased :</strong> 44919 Count<br>
  <strong>NumConnCreated :</strong> 9748 Count<br>
  <strong>NumConnDestroyed :</strong> 9793 Count<br>
  <strong>NumConnFailedValidation :</strong> 70 Count<br>
  <strong>NumConnFree :</strong> 161 Count<br>
  <strong>NumConnUsed :</strong> -136 Count  </p>
  
  <p><strong>How</strong> can the <code>NumConnFree</code> become 161 as I have <code>Maximum Pool Size = 100</code> <strong>?</strong><br>
  <strong>How</strong> can the <code>NumConnUsed</code> become -136, a <em>negative</em> number <strong>?</strong><br>
  <strong>How</strong> can the <code>NumConnDestroyed</code> <strong>></strong> <code>NumConnCreated</code> <strong>?</strong>  </p>