title: Reproduce com.mysql.jdbc.exceptions.jdbc4.CommunicationsException with a setup of Spring, hibernate and C3P0
body: <p>I got this error from the production code:</p>
  <blockquote>
  <p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
  The last packet successfully received
  from the server was36940 seconds
  ago.The last packet sent successfully
  to the server was 36940 seconds ago,
  which 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>And now I'm trying to reproduce the problem locally and fix it. I setup the spring context as following:</p>
  <pre><code>&lt;bean id=&quot;dataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;
      &lt;bean id=&quot;dataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;
      destroy-method=&quot;close&quot; p:driverClass=&quot;com.mysql.jdbc.Driver&quot;
      p:jdbcUrl=&quot;jdbc:mysql://localhost:3306/test?useUnicode=yes&amp;amp;characterEncoding=UTF-8&amp;amp&quot;
      p:idleConnectionTestPeriod=&quot;120&quot; p:initialPoolSize=&quot;1&quot; p:maxIdleTime=&quot;1800&quot;
      p:maxPoolSize=&quot;1&quot; p:minPoolSize=&quot;1&quot; p:checkoutTimeout=&quot;1000&quot;
      
  /&gt;
  
  &lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;&gt;
      &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;
      &lt;property name=&quot;hibernateProperties&quot;&gt;
          &lt;value&gt;
              hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider
              hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
              hibernate.default_schema=platform_server_original
              hibernate.show_sql=false
          &lt;/value&gt;
      &lt;/property&gt;
      &lt;property name=&quot;mappingResources&quot;&gt;
          &lt;list&gt;
              &lt;value&gt;sometables.hbm.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  <p>Then I set my mysql wait_timeout to 10 seconds, then run my test, which is basically open a connection, do a query, close it, so it returns to the pool, then sleep the thread for 15 seconds, and then open a connection again, and do a query again, so it will break. However, I got a similar error only:</p>
  <blockquote>
  <p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
  Communications link failure</p>
  <p>Last packet sent to the server was 16
  ms ago.</p>
  </blockquote>
  <p>So I wonder are these two errors the same, or they are different? I did some researches, and it seems both errors came down to the same solution: using a property &quot;testConnectionOnCheckout=true&quot;. However, according to c3p0 doc, this is a very expensive check. It advises the use of &quot;idleConnectionTestPeriod&quot;, but I'm already setting that to 120 seconds. What value should I use it so it can properly verify the idle connection?</p>
  <p>So I'm basically ask two things:</p>
  <ol>
  <li>how do I reproduce the error I got in production code?</li>
  <li>how do I fix it?</li>
  </ol>
  <p>Thanks!</p>