Does aborting a loop over a Hibernate list leave lingering resources?

Consider the following code to check if the user is in a particular workgroup:

List workgroups = identity.getWorkgroups();
if (workgroups != null) {
    for(Identity wg : workgroups) {
        if (wg.getName().equals("Demo Workgroup")) {
            return true;
        }
    }
}

The list is a lazy-loaded Hibernate collection. The loop aborts midway through iterating over the collection by returning.

Does this leave anything open Hibernate-wise?

In this case I don’t think it leaves anything open because all the Workgroups are already contained in the List object. However, you do need to be careful in cases where you are using context.search() methods which return Iterator objects. These reference the query resultset and should be flushed using the sailpoint.tools.Util.flushIterator(myIterator) method

1 Like

Instead of triggering the lazy init and iterating through all the workgroups, you could do this with quick query instead:

QueryOptions qo = new QueryOptions();
qo.addFilter(Filter.and(Filter.eq("id", identity.getId()), Filter.eq("workgroups.name", "Demo Workgroup"));
boolean found = context.countObjects(Identity.class, qo) == 1;

FYI: This does not use any hibernate cache, or leave any resources open.

3 Likes