com.mysql.jdbc.exceptions.jdbc4.CommunicationsException MySQL + Apache Tomcat 7

Looks like this is the MySQL inactivity timeout - there's quite a few similar StackOverflow questions around it (e.g. here, here and here). Would advise changing your connection pool settings by adding testOnBorrow=true, i.e:

<Resource name="jdbc/devicesDS"
          auth="Container"
          type="javax.sql.DataSource"
          maxActive="10"
          maxIdle="4"
          username="root"
          password="SrbBfsatn640^"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/devices?autoReconnect=true"
          validationQuery="select 1 from dual"
          testOnBorrow=true />

I'd personally lean towards testOnBorrow over the alternative of testWhileIdle since the former is completely independent of the wait_timeout value (but not sure how the performance compares). Would also be cautious about using the alternative of autoreconnect=true in the JDBC URL: It isn't recommended by the MySQL Connector/J documentation "because it has side effects related to session state and data consistency when applications don't handle SQLExceptions properly, and is only designed to be used when you are unable to configure your application to handle SQLExceptions resulting from dead and stale connections properly".


In your application the connection string to database mysql should be:

jdbc:mysql://{host}:{port}/{schema}?autoReconnect=true

adding autoReconnect=true will solve this issue


I run onto this problem with other app implementations, after tryed every config, autoreconnect, etc. The only way I find to solve this was making an schedule event calling a test query every hour to ping the DB, forcing it to refresh the connection.

query("SELECT version()");