title: java.sql.SQLException: Already closed
body: <p>We have a webapp running in production on tomcat with a MySQL back-end. All was fine for sometime, then suddenly we started getting this exception <code>java.sql.SQLException: Already closed.</code></p>
  
  <p>The entire stack trace is:</p>
  
  <pre><code>DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Fetching JDBC Connection from DataSource
  DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Returning JDBC Connection to DataSource
  DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] Could not close JDBC Connection    
  java.sql.SQLException: Already closed.
      at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)
      at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
      at org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:333)
      at org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection(DataSourceUtils.java:294)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:405)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:428)
      at com.nokia.analytics.aws.aggregate.service.importer.DBInsert.truncateTable(DBInsert.java:135)
      at com.blah.analytics.aggregate.service.importer.AggregateCollector.pullAndInsert(AggregateCollector.java:85)
      at com.blah.analytics.aggregate.service.importer.AggregateCollector.call(AggregateCollector.java:96)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:679)
  </code></pre>
  
  <p>We are using <code>org.apache.commons.dbcp.BasicDataSource</code> as our datasource. I searched quite a bit but to no avail.
  It doesn't occur always and hence is very hard to reproduce. It seems a problem with db connection pooling. Somewhere it was suggested to set <a href="http://commons.apache.org/proper/commons-dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html#maxActive" rel="noreferrer">this</a> param as negative. Currently we are not changing of those parameters (all have default vals).</p>
  
  <p>What approach should we follow to avoid it?</p>
  
  <p>EDIT:</p>
  
  <p>The relevant code is in (DBInsert.java)</p>
  
  <p>133:    <code>String sql = "DELETE FROM "+tableName;</code></p>
  
  <p>134:    <code>logger.debug(sql);</code></p>
  
  <p>135:    <code>this.jdbcTemplate.execute(sql);</code></p>
  
  <p>(133-135 are line nos. which are specified in the exception)</p>
  
  <p>My datasource config:</p>
  
  <pre><code>&lt;bean id="bisToolDataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url"
              value="${url}/blah_db?verifyServerCertificate=false&amp;amp;useSSL=true&amp;amp;requireSSL=true" /&gt;
          &lt;property name="username" value="${uname}" /&gt;
          &lt;property name="password" value="${passwd}" /&gt;
      &lt;/bean&gt;
  </code></pre>