title: JDBC Mysql "Communications link failure"
body: <p>I have made a web Server/Application using Tomcat Server 8.5.xx.</p>
  
  <p>I use servlet as the Server Technology. XML-RPC to communicate with the user. and MySQL as a database.</p>
  
  <p>When I run the application (the server), sometimes I become the following error:</p>
  
  <pre><code>06-Sep-2017 12:39:06.260 WARNUNG [commons-pool-EvictionTimer] org.apache.tomcat.dbcp.dbcp2.SwallowedExceptionLogger.onSwallowException An internal object pool swallowed an Exception.
   com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  
  The last packet successfully received from the server was 4.740.870 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:425)
          at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
          at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3746)
          at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1873)
          at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
          at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2194)
          at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2225)
          at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024)
          at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:779)
          at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
          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:425)
          at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
          at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
          at org.apache.tomcat.dbcp.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
          at org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:257)
          at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888)
          at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.ensureIdle(GenericObjectPool.java:952)
          at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.ensureMinIdle(GenericObjectPool.java:931)
          at org.apache.tomcat.dbcp.pool2.impl.BaseGenericObjectPool$Evictor.run(BaseGenericObjectPool.java:1047)
          at java.util.TimerThread.mainLoop(Unknown Source)
          at java.util.TimerThread.run(Unknown Source)
  Caused by: java.net.SocketException: Software caused connection abort: socket write error
          at java.net.SocketOutputStream.socketWrite0(Native Method)
          at java.net.SocketOutputStream.socketWrite(Unknown Source)
          at java.net.SocketOutputStream.write(Unknown Source)
          at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
          at java.io.BufferedOutputStream.flush(Unknown Source)
          at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3728)
          ... 22 more
  </code></pre>
  
  <p>I can provide any data that is needed to solve the problem.</p>
  
  <p>It works and communicates with the DB, but some times it hangs and then give this problem and then it functions another time.</p>
  
  <p>I guess that the problem comes from the idle unused connections that MySQL closes but JDBC does nothing about that and it tries to use them.</p>