我的雄猫池设置出了什么问题?最大空闲似乎不起作用



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

systemctl status postgresql-9.4.service | grep idle -c
284

换句话说:

select count(state) from pg_stat_activity where state like 'idle' 
284

我的上下文中的设置.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;
}

有关我的系统的信息:

服务器版本:Apache Tomcat/9.0.5 服务器构建时间:Feb 6 2018 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

Tomcat jdbc pool定义了minEvictableIdleTimeMillis属性来控制连接必须空闲多长时间才能被视为可逐出。

minEvictableIdleTimeMillis

(int( 对象在符合逐出条件之前在池中可能闲置的最短时间。默认值为 60000(60 秒(。

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

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

timeout 61 watch -n3 "systemctl status postgresql-9.4.service | grep idle -c | tee idle.log"

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

相关内容

最新更新