title: Tomcat JDBC connection pool issue: "Statement is closed"
body: <p>I have a server application that uses the Tomcat JDBC connection pool.</p>
  
  <p>This is the code I use to create the DataSource:</p>
  
  <pre><code>PoolProperties connProperties = new PoolProperties();
  connProperties.setUrl(resources.getProperty("db.url"));
  connProperties.setDriverClassName(resources.getProperty("db.driver"));
  connProperties.setUsername(resources.getProperty("db.user"));
  connProperties.setPassword(resources.getProperty("db.password"));
  connProperties.setJmxEnabled(true);
  connProperties.setTestWhileIdle(false);
  connProperties.setValidationQuery("SELECT 1");
  connProperties.setTestOnReturn(false);
  connProperties.setValidationInterval(30000);
  connProperties.setTimeBetweenEvictionRunsMillis(30000);
  connProperties.setMaxActive(500);
  connProperties.setInitialSize(50);
  connProperties.setMaxWait(10000);
  connProperties.setRemoveAbandonedTimeout(60);
  connProperties.setMinEvictableIdleTimeMillis(60000);
  connProperties.setSuspectTimeout(60);
  connProperties.setMaxIdle(50);
  connProperties.setMinIdle(10);
  connProperties.setLogAbandoned(false);
  connProperties.setRemoveAbandoned(true);
  connProperties.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
  "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
  
  dataSource = new DataSource();
  dataSource.setPoolProperties(connProperties); 
  </code></pre>
  
  <p>Then I have a method to get a connection from the pool</p>
  
  <pre><code>protected Connection getDbConnection() throws Exception
  {
      dbConn = dataSource.getConnection();
      return dbConn;
  }
  </code></pre>
  
  <p>And every time I want to execute a statement I call this code:</p>
  
  <pre><code>protected CallableStatement executeCSqlQuery(String sql) throws Exception
  {
      CallableStatement cstmt;
      ResultSet rs = null;
  
      try {
          cstmt = getDbConnection().prepareCall(sql);     
          cstmt.execute();            
      } catch (SQLException e) {
          throw e;
      }
  
      return cstmt;
  }
  </code></pre>
  
  <p>And this is an example of a call to the previous code:</p>
  
  <pre><code>try {
      cstmt = dbConnection.executeCSqlQuery(query);
      rs = cstmt.getResultSet();
  } catch (Exception e) {
      // do smething
  } finally {
      try {
          if (cstmt != null) {
              cstmt.close();
          }
          dbConnection.shutdown();
      } catch (Exception e) {
          // do something
      }
  }
  
  public void shutdown() {
      if (this.dbConn != null) 
          this.dbConn.close();
  }
  </code></pre>
  
  <p>The problem I'm facing is that every now and then, I'm getting an exception "Statement is closed" when I execute a call in a Thread every X seconds. 
  I'm not sure why this happens. I'm thinking that it could be a driver bug or something failing with the connection to the database (that runs in a different server).</p>
  
  <p>I'm out of ideas. What am I missing?</p>
  
  <p>Should I use <em>c3p0</em> connection pool instead?</p>