【问题标题】:ResultSet.next() is taking too much time for few recordsResultSet.next() 为几条记录花费了太多时间
【发布时间】:2014-11-13 14:11:19
【问题描述】:

我使用OJDBC(v7) 连接到Oracle(11g),在Java。 在某些情况下,在某些大表上,Resultset 无法在适当的时间获取数据。

例如输出记录只有 2 条,但在 Resultset.next() java 上冻结并等待太久!

注意1: 问题不在于设置FetchSize()RsultSet.TypeX,不使用c3p0等连接池,...。 我已经测试了所有这些。

注意2: 另外当我直接在navicat运行查询时,结果显示完美!

获取连接方式:

public Connection getDBConnection() throws DBConnectionException {
        Connection conn = null;

        String connectionUrl;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:user/pass@xxx.xxx.xxx.xxx:1521:DBNAME");

        } catch (Exception e) {
            e.printStackTrace();
            throw new DBConnectionException();
        }

        return conn;
    }

连接到数据库部分:

    ...
    conn = connectionManager.getDBConnection();
    conn.setAutoCommit(false);
    String query = "{call ...(...)}";
    CallableStatement stmt = conn.prepareCall(query,ResultSet.TYPE_FORWARD_ONLY,
                    ResultSet.CONCUR_READ_ONLY);
    stmt.setFetchSize(10000); 
    .
    .
    .
    stmt.registerOutParameter(x, OracleTypes.CURSOR);
    stmt.execute();
    Resultset rs = (ResultSet) stmt.getObject(x);

    while (rs.next()) {    /** Problem occurs here **/
        ...
    }

为什么?!

【问题讨论】:

  • 您是否真的在创建 SP 查询调用后手动 commit() 事务?
  • 您是否比较了在客户端应用程序和 JDBC 中运行时的查询计划?可能是客户端应用使用FIRST_ROWS(x)提示,而JDBC使用ALL_ROWS模式,执行计划不同,客户端返回行比JDBC快。

标签: java oracle resultset ojdbc


【解决方案1】:

在 try catch 块中使用:

if ((cnn==null)||cnn.isClosed()){
    cnn=DB.getDBConnection(); //e.g. DB is instance of class where getDBConnection() resides
}

然后调用查询。我相信在您的代码中的某个地方,您正在关闭连接或连接变为空,这就是您面临此问题的原因。

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2020-07-23
    • 2013-07-11
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多