我正在使用新的Tomcat JDBC连接池(org.apache.tomcat.jdbc.pool
,版本7.0.20)进行一些测试。我对使用验证查询的理解是,当我是重新启动数据库并且池丢失了所有连接,它会自动尝试恢复它们。
这里是初始化代码:
...
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);
...
然后我启动一个计时器,每当它触发时,我从池中获得连接:
...
Connection conn = App.datasource.getConnection();
...
当我启动数据库时,它并没有尝试将连接恢复为已经过时,我总是收到以下异常:
[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.ScheduledThreadPoolExecutor$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)
答案 0 :(得分:0)
最后,我自己发现了这个问题。代码
conn = App.datasource.getConnection();
当抛出异常时,我无法再获得连接。因此,所有操作仍然在旧的,断开的连接上完成。现在我确保conn = App.datasource.getConnection();总是在db操作之前调用,一切都按预期工作。
答案 1 :(得分:-1)
照顾setValidationInterval(30000)
,因为这意味着验证查询将每30秒完成一次。我认为将此设置为0更安全,以便在每次请求连接时触发验证查询。