title: Percona connection fails after timeout
body: <p>We have multimodule project with database, and sometimes we recieve this exception:</p>
  
  <pre><code>org.springframework.dao.DataAccessResourceFailureException: PreparedStatementCallback; SQL [SELECT data FROM table]; No operations allowed after connection closed.; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
  at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:79)
  ...
  at java.lang.Thread.run(Thread.java:745)
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
  at sun.reflect.GeneratedConstructorAccessor108.newInstance(Unknown Source)
  ... 87 common frames omitted
  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 51,072,384 milliseconds ago.  The last packet sent successfully to the server was 51,072,384 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
  at sun.reflect.GeneratedConstructorAccessor89.newInstance(Unknown Source)
  ... 78 common frames omitted
  Caused by: java.net.SocketException: Broken pipe
  at java.net.SocketOutputStream.socketWrite0(Native Method)
  ... 83 common frames omitted
  </code></pre>
  
  <p>Google suggested that it could be because of database closes connections when no queries in time specified in wait_timeout variable. 
  So I reprodused this situation in that way:</p>
  
  <p>Use Percona server 5.7.16, SpringBoot 1.3.3</p>
  
  <p>Set timeouts in mysql to demonstrate problem (in production sets default value 8 hours):</p>
  
  <pre><code>SET GLOBAL interactive_timeout = 15;
  SET GLOBAL wait_timeout = 15;
  </code></pre>
  
  <p>Set heartbeat for mysql in springboot properties:</p>
  
  <pre><code>spring.datasource.validationQuery=SELECT 1
  spring.datasource.testWhileIdle = true
  </code></pre>
  
  <p>Run test:</p>
  
  <pre><code>@Test
  @DatabaseSetup("/....xml")
  public void test() throws Exception {
      mockMvc
          .perform(get("/request_that_fetches_data_from_db"))
          .andExpect(status().isOk());
  
      sleep(1000 * 60);
      mockMvc
           .perform(get("/request_that_fetches_data_from_db"))
          .andExpect(status().isOk());
  
  }
  </code></pre>
  
  <p>Result is:</p>
  
  <pre><code>java.sql.SQLException: Could not retrieve transation read-only status server
  
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
  ...
  
  Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  The last packet successfully received from the server was 40 414 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  ...
  Caused by: 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:2957)
  ... 46 more
  </code></pre>
  
  <p>I looked at mysql general log. There are query <code>SELECT 1</code> passes every 10 seconds, but when after that recieve business query, then straight recieve command <code>quit</code>. Error log was empty for testing period.</p>
  
  <p>What is wrong and how to fix this problem without increasing wait_timeout?</p>
  
  <p>P.S.<code>jdbc:mysql://url?autoReconnect=true</code> don't works too.</p>