Loader leaks memory when ResultSet wrapping is enabled

Description

This issue was already reported on the forums (http://forum.hibernate.org/viewtopic.php?t=968578), but I can find no actual JIRA entry. The issue is that when the hibernate.jdbc.wrap_result_sets property is set to true, ResultSets are never removed from the AbstractBatcher's resultSetsToClose set, eventually causing memory to fill up with ResultSet objects and their associated resources (like PreparedStatements).
This issue was witnessed in 3.2.6, but a look at the 3.3 CR1 source code reveals that it's still in there.

What happens:

1) org.hibernate.loader.Loader.doQuery calls org.hibernate.loader.Loader.getResultSet

2) org.hibernate.loader.Loader.getResultSet calls Batcher.getResultSet. The AbstractBatcher implementation creates a ResultSet by executing the query and adds it to the AbstractBatcher's resultSetsToClose Set

3) Loader then calls its wrapResultSetIfEnabled method to wraps the ResultSet in a ResultSetWrapper if the wrap_result_sets option is enabled.

4) In Loader.doQuery's finally block, Batcher.closeQueryStatement is called with the PreparedStatement and the wrapped ResultSet as parameters

5) closeQueryStatement does:

if ( resultSetsToClose.remove( rs ) ) {
logCloseResults();
rs.close();
}

6) resultSetsToClose.remove(rs) will always return false if the ResultSet was wrapped, as ResultSetWrapper does not override equals()/hashCode() (and therefore inherits Object's equals() and hashCode()). This means the reference to the original (unwrapped) ResultSet remains (is not removed from the Set) and the ResultSet is not closed either as a result.

Workaround: Set hibernate.jdbc.wrap_result_sets to false

Solution: implement ResultSetWrapper.equals()/hashCode() (see attached ResultSetWrapper.java)

Attachments

1

Activity

Show:

Brett Meyer March 7, 2014 at 5:30 PM

Bulk closing rejected tickets in "resolved" state.

Gérald Quintana April 5, 2011 at 11:50 PM

Obviously, this issue is fixed since 3.5 release when ResultSetWrapper was replaced by ResultSetWrapperProxy

Strong Liu March 28, 2011 at 5:20 PM

I believe this is not a problem anymore in the current hibernate release

March 25, 2010 at 11:47 AM

The proposed solution (implementing equals and hash code) worked for me.

Rejected

Assignee

Strong Liu

Reporter

Components

Affects versions

Priority

Created June 26, 2008 at 6:57 PM
Updated March 7, 2014 at 5:30 PM
Resolved March 28, 2011 at 5:20 PM
Loading...