I have a server application that uses the Tomcat JDBC connection pool.
This is the code I use to create the DataSource:
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);
Then I have a method to get a connection from the pool
protected Connection getDbConnection() throws Exception
{
dbConn = dataSource.getConnection();
return dbConn;
}
And every time I want to execute a statement I call this 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;
}
And this is an example of a call to the previous 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();
}
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).
I'm out of ideas. What am I missing?
Should I use c3p0 connection pool instead?