Postgresql 我的tomcat池设置有什么问题?它似乎不起作用

Postgresql 我的tomcat池设置有什么问题?它似乎不起作用,postgresql,tomcat,connection-pooling,Postgresql,Tomcat,Connection Pooling,服务器上的postgresql状态显示的空闲数大于tomcat中配置的空闲数 systemctl status postgresql-9.4.service | grep idle -c 284 换言之: select count(state) from pg_stat_activity where state like 'idle' 284 my context.xml中的设置为: <?xml version="1.0" encoding="UTF-8"?> <Cont

服务器上的postgresql状态显示的空闲数大于tomcat中配置的空闲数

systemctl status postgresql-9.4.service | grep idle -c
284
换言之:

select count(state) from pg_stat_activity where state like 'idle' 
284
my context.xml中的设置为:

<?xml version="1.0" encoding="UTF-8"?>
<Context  allowCasualMultipartParsing="true">
    <Resource 
               name="jdbc/postgres" 
               auth="Container"
               type="javax.sql.DataSource" 
               driverClassName="org.postgresql.Driver"
               url="jdbc:postgresql://localhost:5432/db1"
               username="postgres" password="*****" 
               initialSize ="30"
               maxTotal="300" maxIdle="20" maxWaitMillis="30000"
               closeMethod="close"
               validationQuery="SELECT 1"
               validationQueryTimeout="5"
               removeAbandonedOnBorrow="true"
               removeAbandonedOnMaintenance="true"
               removeAbandonedTimeout="60"
               logAbandoned="true"
      />

          <Resource 
               name="jdbc/postgresDb2" 
               auth="Container"
               type="javax.sql.DataSource" 
               driverClassName="org.postgresql.Driver"
               url="jdbc:postgresql://localhost:5432/db2"
               username="postgres" password="*****" 
               initialSize ="30"
               maxTotal="300" maxIdle="20" maxWaitMillis="30000"
               closeMethod="close"
               validationQuery="SELECT 1"
               validationQueryTimeout="5"
               removeAbandonedOnBorrow="true"
               removeAbandonedOnMaintenance="true"
               removeAbandonedTimeout="60"
               logAbandoned="true"
      />
</Context>
我的代码中的连接如下所示:

 public String asignacionMax(String type) throws ModuleException, Exception{      
    Connection conn = null;
    PreparedStatement stmt = null;  
    String accountno="";
    try{
        String query = "select nextval('public.asignacionseq');";
        conn = getConnection("GU");
        stmt = conn.prepareStatement(query);   
        ResultSet rst = stmt.executeQuery();    
        String cnt="";
        if(rst.next()){
            cnt = rst.getString("nextval");
            for(int i=cnt.length();i<9;i++){
                cnt= "0"+cnt;
            }   
        }
        accountno = type+cnt;

    }catch(Exception e){
        throw new Exception(e);
    }finally{
        if (conn != null) {conn.close();}
        if (stmt != null) {stmt.close();}
    }
    return accountno;
}
有关我的系统的信息:

服务器版本:ApacheTomcat/9.0.5 服务器构建时间:2018年2月6日21:42:23 UTC 服务器编号:9.0.5.0 操作系统名称:Linux 操作系统版本:3.10.0-693.21.1.el7.x86_64 架构:amd64 JVM版本:1.8.0_161-b14定义了minEvictableIdleTimeMillis属性,以控制连接必须空闲多长时间才能被视为可收回

地雷

int对象在符合逐出条件之前在池中处于空闲状态的最短时间。默认值为60000 60秒

您的配置未指定此属性,因此默认情况下为60秒

1在此期间应计算空闲连接数,以检查其变化。 2定义了两个池,因此在某个点上的空闲连接数可能是空闲连接数的两倍。 默认情况下,每5秒检查3个可收回连接,因此您可以在61秒内每3秒检查一次:

idle.log应包含在该期间找到的值。
问题的另一个方面是确保您的验证方法从池的角度来看是适当的,换句话说,如果idle对两者都意味着相同的话。

为了给您一个很好的答案,如果您还没有了解,那么您可以浏览一下。如果你能提供一个简单的答案,这可能会很有用。谢谢你的回答。在阅读了建议的主题后,我做了一些改变。我正在测试你的建议,这个数字从不减少,而是增加。关于这个意思,我的观点是idle是一样的,因为我们在这两种情况下都在讨论数据库。现在我在代码中添加了一个-a,以便更好地检查它是否减少了。。。我看到它通常减少1,然后又增加。但每次我检查时,这个数字总是在上升。大约两个小时后,这个数字下降到上次的50%。但它继续增加,tomcat中配置的代码数量仍然不同。关于您的代码,我认为stmt在conn之前就关闭了。无论如何,这可能是连接泄漏。可能就是这个原因。但我的问题是澄清tomcat池中配置的数字是否与数据库中发生的情况不一致。以及为什么忽略该配置,数字会继续增加。
timeout 61 watch -n3 "systemctl status postgresql-9.4.service | grep idle -c | tee idle.log"