0
votes

I have rather a simple portlet which displays the number of visitors of portal (online, daily, weekly, monthly, annually).

In the portlet class in doView method firstly I call one method which do an insert to the table (info about a new visitor). After I call 5 methods one by one which do a count select on the same table. They are all pretty similar, only their queries differ. One of the methods implementation is the following:

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;
}

Connection is obtained:

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;
}

Connection is closed in the end of doView method. Occasionally I'm getting that exception:

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

from one or few of the methods which do select. Also the following errors sometimes:

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

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

After searching the internet I still haven't found/realized what is the cause of an error in my case and how I can fix it. Any help would be appreciated.

2

2 Answers

0
votes

Set resultSetHoldability either at driver level or when preparing your statement. This should fix what you are experiencing.

0
votes

The reason that I got the same error was because I closed the db2 connection and then tried to read the result set. Please put some emphasis on the fact that the db2 must remain connected while reading the result set.