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

问题描述 投票:0回答:2

我有一个相当简单的 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.

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

java db2 websphere-7
2个回答
0
投票

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


0
投票

我得到同样错误的原因是因为我关闭了db2连接,然后尝试读取结果集。请强调以下事实:读取结果集时 db2 必须保持连接。

© www.soinside.com 2019 - 2024. All rights reserved.