title: Hibernate c3p0 connection pool not timing out idle connections
body: <p>We have a java server connecting to a MySQL 5 database usingHibernate as our persistence layer which is using c3p0 for DB connection pooling.</p>
  
  <p>I've tried following the c3p0 and hibernate documentation:</p>
  
  <ul>
  <li><a href="https://www.hibernate.org/214.html" rel="noreferrer">Hibernate - HowTo Configure c3p0 connection pool</a></li>
  <li><a href="http://www.mchange.com/projects/c3p0/index.html#hibernate-specific" rel="noreferrer">C3P0 Hibernate properties</a>
  
  <ul>
  <li><a href="http://www.mchange.com/projects/c3p0/index.html#configuration_properties" rel="noreferrer">C3P0.properties configuration</a></li>
  </ul></li>
  </ul>
  
  <p>We're getting an error on our production servers stating that:</p>
  
  <blockquote>
    <p>... Caused by:
    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
    No operations allowed after connection
    closed.Connection was implicitly
    closed due to underlying
    exception/error:</p>
    
    <p>BEGIN NESTED EXCEPTION </p>
    
    <p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException</p>
    
    <p>MESSAGE: The last packet successfully
    received from the server was45000
    seconds ago.The last packet sent
    successfully to the server was 45000
    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>
    
    <p>STACKTRACE:</p>
    
    <p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
    The last packet successfully received
    from the server was45000 seconds
    ago.The last packet sent successfully
    to the server was 45000 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>We have our c3p0 connection pool properties setup as follows:</p>
  
  <pre><code>hibernate.c3p0.max_size=10
  hibernate.c3p0.min_size=1
  hibernate.c3p0.timeout=5000
  hibernate.c3p0.idle_test_period=300
  hibernate.c3p0.max_statements=100
  hibernate.c3p0.acquire_increment=2
  </code></pre>
  
  <p>The <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_wait_timeout" rel="noreferrer">default MySQL wait_timetout</a> is set to 28800 seconds (8 hours), the reported error is saying that it's been over 45000 seconds (about 12.5 hours). Although the c3p0 configuration states that it will "timeout" idle connections that haven't been used after 5000 seconds and it will check every 300 seconds, thus an idle connection should never live longer than 5299 seconds right?</p>
  
  <p>I've tested locally by setting my developer MySQL (my.ini on windows, my.cnf on Unix) wait_timeout=60 and lowering the c3p0 idle timeout values below 60 seconds, and it will properly timeout idle connections and create new ones. I also check to ensure that we're not leaking DB connections and holding onto a connection, and it doesn't appear we are.</p>
  
  <p>Here's the c3p0.properties file I'm using to test in my developer environment to ensure c3p0 is properly handling connections.</p>
  
  <p><em>hibernate.properties (testing with MySQL wait_timeout=60)</em></p>
  
  <pre><code>hibernate.c3p0.max_size=10
  hibernate.c3p0.min_size=1
  hibernate.c3p0.timeout=20
  hibernate.c3p0.max_statements=100
  hibernate.c3p0.idle_test_period=5
  hibernate.c3p0.acquire_increment=2
  </code></pre>
  
  <p><em>c3p0.properties</em></p>
  
  <pre><code>com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=ALL
  com.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog
  c3p0.debugUnreturnedConnectionStackTraces=true
  c3p0.unreturnedConnectionTimeout=10
  </code></pre>