title: JAVA JPA persistence.xml - how to test connection validity before use in my application
body: <p>I use Google Guice Persist ( JPA ) to operate the data. My web application is on the Jetty server. Following is my persistence.xml：</p>
  <pre><code>    &lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;
               xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
               xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/persistence
                                   http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd&quot;
               version=&quot;2.0&quot;&gt;
  
      &lt;persistence-unit name=&quot;MyJpaUnit&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
  
          &lt;class&gt;my.api.repository.entity.Foo&lt;/class&gt;
          &lt;shared-cache-mode&gt;NONE&lt;/shared-cache-mode&gt;
  
          &lt;properties&gt;
              &lt;property name=&quot;javax.persistence.jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.url&quot;
                        value=&quot;jdbc:mysql://127.0.0.1/testing?characterEncoding=UTF-8&amp;amp;zeroDateTimeBehavior=convertToNull&amp;amp;autoReconnect=true&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.user&quot; value=&quot;test_account&quot; /&gt;
              &lt;property name=&quot;javax.persistence.jdbc.password&quot; value=&quot;test_pwd&quot; /&gt;
              &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.MySQLDialect&quot; /&gt;
              &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;false&quot;/&gt;
              &lt;property name=&quot;format_sql&quot; value=&quot;false&quot;/&gt;
  
              &lt;property name=&quot;hibernate.cache.use_second_level_cache&quot; value=&quot;true&quot;/&gt;
              &lt;property name=&quot;hibernate.cache.use_query_cache&quot; value=&quot;false&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.max_size&quot; value=&quot;30&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.min_size&quot; value=&quot;5&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.acquire_increment&quot; value=&quot;1&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.idle_test_period&quot; value=&quot;300&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.max_statements&quot; value=&quot;0&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.timeout&quot; value=&quot;100&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.preferredTestQuery&quot; value=&quot;SELECT 1&quot; /&gt;
              &lt;property name=&quot;hibernate.c3p0.testConnectionOnCheckout&quot; value=&quot;true&quot; /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  <p>I got the following error when my application was idle：</p>
  <blockquote>
  <p>[c3p0] Another error has occurred [ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 49,492,938 milliseconds ago.  The last packet sent successfully to the server was 49,492,938 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. ] which will not be reported to listeners!</p>
  <p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 49,492,938 milliseconds ago.  The last packet sent successfully to the server was 49,492,938 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.</p>
  </blockquote>
  <p>How can i configure the persistence.xml to avoid this problem ?
  Thanks.</p>