title: Spring ORM with C3P0 or DBCP is leaking connections
body: <p>On the latest versions of both DBCP and C3P0, using Spring's Ibatis support,  I am running into an issue where both leak connections. </p>
  
  <p>The scenario is that there is a log running SQL which locks multiple tables. This causes the connections in my pool to max out as users trigger queries which hit the locked tables. Finally, the administrator goes into MySQL and does a <code>kill query &lt;id&gt;</code> on the long running SQL. </p>
  
  <p>If there is enough threads (in my case around 50 or more) which are waiting around for a DB thread to be checked back in to the pool, then I see something like the following in a thread dump:</p>
  
  <pre><code> java.lang.Thread.State: WAITING (on object monitor)
      at java.lang.Object.wait(Native Method)
      at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1315)
      at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
      - locked &lt;0x00002aaacbb01118&gt; (a com.mchange.v2.resourcepool.BasicResourcePool)
      at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
      at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
      at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
      at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
      at 
  </code></pre>
  
  <p>or </p>
  
  <pre><code>  java.lang.Thread.State: WAITING (on object monitor)
      at java.lang.Object.wait(Native Method)
      at java.lang.Object.wait(Object.java:485)
      at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1104)
      - locked &lt;0x00002aab0f030620&gt; (a org.apache.commons.pool.impl.GenericObjectPool$Latch)
      at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
      at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
      at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
      at 
  </code></pre>
  
  <p>and these threads wait around FOREVER. </p>
  
  <p>This does not happen if the pool is maxed out, and only a few (around 5) threads are waiting for a free connection in the pool. </p>
  
  <p>I know there is configuration that can fix this problem (setting timeout etc.), but I am interested in why this is happening in the first place? Why are the active threads not being returned to the pool, when there are 50 or more threads waiting for a connection and I kill the long running SQL? </p>
  
  <blockquote>
    <p><strong>Update:</strong> I should have made it clear that I am using Spring 3.0.2 and
    Ibatis 2.3. I use the SqlMapClientTemplate, which manages my
    connections for me. At this point, I am beginning to think it is
    Ibatis 2.3 not handling heavy load correctly.</p>
  </blockquote>