com.ibm.db2.jcc.am.SqlException:无效操作:结果集已关闭。 ERRORCODE = -4470,SQLSTATE = null

时间:2013-03-06 04:52:00

标签: java db2 websphere-7

我有一个简单的portlet,它显示门户网站的访问者数量(在线,每日,每周,每月,每年)。

在doView方法的portlet类中,我首先调用一个对表执行插入的方法(有关新访问者的信息)。在我逐个调用5个方法后,在同一个表上进行计数选择。它们都非常相似,只是它们的查询不同。其中一种方法实现如下:

public static Integer getOnline() {
    Integer res = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
        stmt = getConnection().createStatement();
        rs = stmt.executeQuery(query);
        if (rs.next()) {
           res = new Integer(rs.getString("1"));
        }
    } catch (SQLException e) {
        log.error("Excepton: " + e);
    } finally {
        if (rs != null) {
            try { rs.close(); } catch (SQLException e) { log.warn("Error closing result set: ", e); }
            rs = null;
        }

        if (stmt != null) {
            try { stmt.close(); } catch (SQLException e) { log.warn("Error closing statement: ", e); }
            stmt = null;
        }
    }

    return res;
}

获得连接:

public static Connection getConnection() {
    try {
        if (connection == null) {
            if (dataSource == null) {
                dataSource = (DataSource) new InitialContext().lookup(dataSourceName);
            }

            connection = dataSource.getConnection();
        }
    } catch (Exception e) {
        log.error("Error on opening a connection: ", e);
    }

    return connection;
}

在doView方法结束时关闭连接。偶尔我会得到那个例外:

com.ibm.db2.jcc.am.SqlException: [jcc][t4][10120][10898][4.14.88] Invalid operation: result set is closed. ERRORCODE=-4470, SQLSTATE=null

从一个或几个选择的方法。还有以下错误:

com.ibm.websphere.ce.cm.ObjectClosedException: DSRA9110E: Connection is closed.

com.ibm.websphere.ce.cm.ObjectClosedException: DSRA9110E: Statement is closed.

在搜索互联网后,我仍然没有找到/意识到我的情况是错误的原因是什么以及我如何解决它。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

driver级别或preparing语句时设置resultSetHoldability。这应该可以解决您遇到的问题。

答案 1 :(得分:0)

我得到相同错误的原因是因为我关闭了db2连接然后尝试读取结果集。请注意db2在读取结果集时必须保持连接的事实。