javacachedrowset

CachedRowSet :: release() vs. close()


I am using a javax.sql.rowset.CachedRowSet object which I pass around in my code. The ResultSet from which it was populated is closed just before the CachedRowSet is passed to the rest of my code (only for reading, I do not update or modify it in any way):

public CachedRowSet getData(String query) throws SQLException {
    RowSetFactory aFactory = RowSetProvider.newFactory();
    CachedRowSet crs = aFactory.createCachedRowSet();
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        crs.populate(rs);
    } finally {
        DbUtils.closeQuietly(null, stmt, rs);
    }
    return crs;
}

Given the above setup, does it make sense to also close() or release() the CachedRowSet object when I am done with it? This answer seems to suggest that close() doesn't hurt but doesn't address release(), or the difference between the two. I suppose I could do a release() followed by a close() just in case but I would like to understand a bit more. Note that I even serialize the CachedRowSet object and then internalize it from a String in an other module so I could also call close() and release() on the re-internalized instance which doesn't make any sense.


Solution

  • I dont think i totally understand your question. But..

    realese() -> Releases the current contents of this CachedRowSet object and sends a rowSetChanged event to all registered listeners. Any outstanding updates are discarded and the rowset contains no rows after this method is called. There are no interactions with the underlying data source, and any rowset content, metadata, and content updates should be non-recoverable. Link:(http://docs.oracle.com/javase/7/docs/api/javax/sql/rowset/CachedRowSet.html)

    close() -> It inherited from the ResultSet Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

    The closing of a ResultSet object does not close the Blob, Clob or NClob objects created by the ResultSet. Blob, Clob or NClob objects remain valid for at least the duration of the transaction in which they are creataed, unless their free method is invoked.

    When a ResultSet is closed, any ResultSetMetaData instances that were created by calling the getMetaData method remain accessible.

    Note: CachedRowSet not keep the connection alive. It's just needs a connection while it performs a query or update.

    Hope It helps :)