tomcat jdbc池超时无法正常工作

时间:2014-07-21 14:55:55

标签: java oracle tomcat jdbc connection-timeout

我正在使用 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();

            }
        }

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

1 个答案:

答案 0 :(得分:0)

尝试使用此配置:

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