title: Running out of DB connections!
body: <p>I'm running a Spring/Hibernate connecting to MySQL setup using c3p0 as my connection pool. For some bizarre reason it runs out of connections when the system is under load (of course).</p>
  
  <p>The site was pretty stable until we started hitting a new level of traffic (over a hundred concurrent users). At that point the DB would melt down (peg the CPU). My first action was in the application to improve performance through extensive caching and optimization of queries etc. </p>
  
  <p>Now it will just run out of connections intermittently. It doesn't even seem that dependent on load. More on time which makes me think it's a leak but for the life of me I can't figure out where it would be coming from.</p>
  
  <pre><code>    WARN [2011-03-07 17:19:42,409] [TP-Processor38] (JDBCExceptionReporter.java:100) - SQL Error: 0, SQLState: null
  ERROR [2011-03-07 17:19:42,409] [TP-Processor38] (JDBCExceptionReporter.java:101) - An attempt by a client to checkout a Connection has timed out.
  ERROR [2011-03-07 17:19:42,410] [TP-Processor38] (HttpHeadFilter.java:46) - There was a problem passing thru filter:/is-this-guy-crazy-or-just-a-huge-dancing-with-the-stars-fan
  org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query
          at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
          at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
          at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)
          at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
  
  Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
      at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
      at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
      at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:527)
      at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
  </code></pre>
  
  <p>Here's my configuration:</p>
  
  <pre><code> &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"&gt;
          &lt;property name="targetDataSource" ref="rootDataSource" /&gt;
      &lt;/bean&gt;
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="mappingLocations" value="classpath:hibernate-mapping.xml" /&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.connection.provider_class"&gt;net.sf.hibernate.connection.C3P0ConnectionProvider&lt;/prop&gt;
                  &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.generate_statistics"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.cache.provider_class"&gt;net.sf.ehcache.hibernate.EhCacheProvider&lt;/prop&gt;
                  &lt;prop key="hibernate.generate_statistics"&gt;${hibernate.generate_statistics}&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.zeroDateTimeBehavior"&gt;convertToNull&lt;/prop&gt;
                  &lt;prop key="hibernate.bytecode.use_reflection_optimizer"&gt;${hibernate.bytecode.use_reflection_optimizer}&lt;/prop&gt;
                  &lt;!--&lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;--&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;${hibernate.jdbc.batch_size}&lt;/prop&gt;
  
                  &lt;!--Actually, it seems the following property affects batch size (or explicit per relationship in the mapping)--&gt;
                  &lt;!--&lt;prop key="hibernate.default_batch_fetch_size"&gt;${hibernate.jdbc.batch_size}&lt;/prop&gt;--&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="rootDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;
          &lt;property name="driverClass" value="${jdbc.driver}" /&gt;
          &lt;property name="jdbcUrl" value="${jdbc.url}" /&gt;
          &lt;property name="user" value="${jdbc.username}" /&gt;
          &lt;property name="password" value="${jdbc.password}" /&gt;
          &lt;property name="initialPoolSize" value="20" /&gt;
          &lt;property name="maxPoolSize" value="200" /&gt;
          &lt;property name="checkoutTimeout" value="30000" /&gt;
          &lt;property name="maxStatements" value="180" /&gt;
  
          &lt;property name="minPoolSize"&gt;
              &lt;value&gt;${hibernate.c3p0.minPoolSize}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="acquireRetryAttempts"&gt;
              &lt;value&gt;${hibernate.c3p0.acquireRetryAttempts}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="acquireIncrement"&gt;
              &lt;value&gt;${hibernate.c3p0.acquireIncrement}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="idleConnectionTestPeriod"&gt;
              &lt;value&gt;${hibernate.c3p0.idleConnectionTestPeriod}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="maxIdleTime"&gt;
              &lt;value&gt;${hibernate.c3p0.maxIdleTime}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="maxIdleTimeExcessConnections"&gt;
              &lt;value&gt;${hibernate.c3p0.maxIdleTimeExcessConnections}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="maxConnectionAge"&gt;
              &lt;value&gt;${hibernate.c3p0.maxConnectionAge}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="preferredTestQuery"&gt;
              &lt;value&gt;${hibernate.c3p0.preferredTestQuery}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="testConnectionOnCheckin"&gt;
              &lt;value&gt;${hibernate.c3p0.testConnectionOnCheckin}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="numHelperThreads"&gt;
              &lt;value&gt;${hibernate.c3p0.numHelperThreads}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="unreturnedConnectionTimeout"&gt;
              &lt;value&gt;${hibernate.c3p0.unreturnedConnectionTimeout}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="debugUnreturnedConnectionStackTraces"&gt;
              &lt;value&gt;${hibernate.c3p0.debugUnreturnedConnectionStackTraces}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="automaticTestTable"&gt;
              &lt;value&gt;${hibernate.c3p0.automaticTestTable}&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      hibernate.c3p0.acquireIncrement=5
  hibernate.c3p0.minPoolSize=20
  hibernate.c3p0.acquireRetryAttempts=30
  hibernate.c3p0.idleConnectionTestPeriod=3600
  hibernate.c3p0.maxIdleTime=7200
  hibernate.c3p0.maxIdleTimeExcessConnections=1800    
  hibernate.c3p0.maxConnectionAge=14400
  hibernate.c3p0.preferredTestQuery=select 1;
  hibernate.c3p0.testConnectionOnCheckin=false
  hibernate.c3p0.numHelperThreads=6
  hibernate.c3p0.unreturnedConnectionTimeout=0
  hibernate.c3p0.debugUnreturnedConnectionStackTraces=true
  hibernate.c3p0.automaticTestTable=test_connection;
  </code></pre>
  
  <p>I am running OpenSessionInViewInterceptor which should be closing the connections:</p>
  
  <pre><code> &lt;bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"&gt;
      &lt;property name="sessionFactory"&gt;
          &lt;ref bean="sessionFactory" /&gt;
      &lt;/property&gt;
      &lt;property name="flushModeName"&gt;
          &lt;value&gt;FLUSH_AUTO&lt;/value&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  </code></pre>
  
  <p>I am also using the spring annotations for @Transactional since I reuse my services in non web fronted code. </p>
  
  <p>There are really only two options here, it's not releasing connections when done. Or it's hanging out chatting up the db like it's trying to get in her pants.
  If anyone has any ideas I would be grateful
  thx</p>
  
  <p><b>FOLLOW UP:</b> In the end it turns out I was leaking connections due to using OpenSessionInViewInterceptor. I had spring security running as a filter so it would connect to the DB and never close them. The fix was to move the OpenSessionInViewInterceptor to OpenSessionInViewFilter.</p>