gpt4 book ai didi

java - tomcat jdbc 池超时不工作

转载 作者:行者123 更新时间:2023-11-28 22:02:41 24 4
gpt4 key购买 nike

我正在使用tomcat jdbc 连接池Oracle 数据库开发高负载应用程序。确保我的应用程序具有非常小的数据库查询超时(不超过 3 秒)非常重要,以防止长时间运行的查询或数据库缓慢阻塞我的所有应用程序。为了模拟长时间运行的查询,我使用 ALTER SYSTEM QUIESCE RESTRICTED 语句将数据库置于 QUIESCE 状态。

但看起来超时值没有影响 - 当我开始测试我的应用程序时,它挂起......

这是我的 jdbc 池配置:

String connprops = "oracle.net.CONNECT_TIMEOUT=3000;oracle.jdbc.ReadTimeout=3000;"
+ "oracle.net.READ_TIMEOUT=3000";


pp.setConnectionProperties(connprops);

pp.setDriverClassName("oracle.jdbc.OracleDriver");

pp.setTestOnBorrow(true);
pp.setTestOnConnect(true);
pp.setTestOnReturn(true);

pp.setTestWhileIdle(true);

pp.setMaxWait(2000);
pp.setMinEvictableIdleTimeMillis(20000);
pp.setTimeBetweenEvictionRunsMillis(20000);

pp.setValidationInterval(3000);
pp.setValidationQuery("SELECT 1 FROM DUAL");

pp.setMaxAge(3000);
pp.setRemoveAbandoned(true);
pp.setRemoveAbandonedTimeout(3);

pp.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.QueryTimeoutInterceptor(queryTimeout=3)");
dataSource = new DataSource();
dataSource.setPoolProperties(pp);

这就是我处理连接的方式(非常简单):

Connection conn = dataSource.getConnection();
Statement stmt = null;
ResultSet rs = null;

try {
stmt = conn.createStatement();

rs = stmt.executeQuery(/*some select query*/);


if (rs.next()) {

result = rs.getInt(1);

/*process the result*/

}

rs.close();
stmt.close();
conn.close();

}
catch(Exception e) {
logger.error("Exception: " + e.getMessage(), e);
}finally {
if (conn != null) {

if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
conn.close();

}
}

有什么想法吗?提前致谢!

最佳答案

尝试使用这个配置:

String connprops = "oracle.net.CONNECT_TIMEOUT=\"3000\";oracle.jdbc.ReadTimeout=\"3000\";"
+ "oracle.net.READ_TIMEOUT=\"3000\"";

java.util.Properties.java 忽略所有非字符串值:

public String getProperty(String key) {
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null; // <- !!!!
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}

您可能还应该使用 java.sql.Statement's query timeout :

stmt.setQueryTimeout(3); // int seconds

关于java - tomcat jdbc 池超时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com