开发者

Validation Query in org.apache.tomcat.jdbc.pool

开发者 https://www.devze.com 2023-03-30 12:50 出处:网络
I\'m recntly doing some tests with the new Tomcat JDBC connection pool (org.apache.tomcat.jdbc.pool, version 7.0.20). My understanding of using the validation query is, when I\'m e.g. rebooting the da

I'm recntly doing some tests with the new Tomcat JDBC connection pool (org.apache.tomcat.jdbc.pool, version 7.0.20). My understanding of using the validation query is, when I'm e.g. rebooting the database and the pool looses all connections, it automatically tries to recover them.

Here the initialization Code:

...

PoolProperties p = new PoolProperties();
p.setUrl(connString);
p.setDriverClassName("org.postgresql.Driver");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT version();");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(maximumDbConnections);
p.setInitialSize(1);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(minimumIdleDbConnections);
p.setMaxIdle(maximumIdleDbConnections);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setInitSQL("SET application_name = 'My Server'");
datasource = new DataSource();
datasource.setPoolProperties(p);

...

In then I start a timer and everytime it fires, I get the connection from the pool:

...

Connection conn = App.datasource.getConnection();

...

When Im booting the database, it's not trying to revover the connections as expacted, I always receive the following exception:

[2011-08-22 23:50:53,066][871009][ERROR]{DbPollThread - 1}  [0144] SQLException while checking for hangig jobs
java.sql.SQLException: Connection has already been closed.
    at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:118)
    at $Proxy2.prepareStatement(Unknown Source)
    at org.voxtelo.faxserver.internal.database.FaxDatabaseHandler.resetOrDeactivateHangingJobs(FaxDatabaseHandler.java:404)
    at org.voxtelo.faxserver.internal.database.FaxDatabaseHandler.pollJobsFromDB(FaxDatabaseHandler.java:884)
    at org.voxtelo.faxserver.internal.database.FaxDatabaseHandler.access$1(FaxDatabaseHandler.java:882)
    at org.voxtelo.faxserver.internal.database.FaxDatabaseHandler$PollDatabaseTask.run(FaxDatabaseHandler.java:940)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
    at java.util.concurrent.ScheduledT开发者_运维技巧hreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:204)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)


Finally I found out the issue by my self. The code

conn = App.datasource.getConnection();

where I'm getting the connection was not reached anymore when the exception was thrown. So all oprations were still done on the old, broken connection. Now I make sure that conn = App.datasource.getConnection(); is always called before a db operation and everything works as expected.


Take care of setValidationInterval(30000), because it means that the validation query will be done each 30 seconds. I think it's safer to set this with 0 to fire the validation query every time is requested a connetion.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号