When scrolling over nested scrollable resultsets some entities remain referenced even when evicted from the session.
From what I’ve gathered from the code the ScrollableResultsConsumer registers the jdbcValuesSourceProcessingState in the LoadContexts . But deregister is never called with the jdbcValuesSourceProcessingState like it is in the ListResultsConsumer. This is probably fine if you just have one Stream/ScrollableResults but if you have nested streams it can become a problem.
final LoadContexts loadContexts =
((SessionImplementor)session).getPersistenceContext().getLoadContexts();
final Field stackField =
LoadContexts.class.getDeclaredField("jdbcValuesSourceProcessingStateStack");
stackField.setAccessible(true);
final StandardStack<JdbcValuesSourceProcessingState> stack =
(StandardStack<JdbcValuesSourceProcessingState>)stackField.get(loadContexts);
try (Stream<Event> events = session.createQuery("from Event", Event.class).stream()) {
events.forEach(event -> {
try (Stream<Item> items = session.createQuery("from Item", Item.class).stream()) {
items.forEach(item -> {
System.out.println("Item (" + item.getName() + ")");
session.evict(item);
});
}
session.evict(event);
});
}
System.out.println(stack.depth());
In this example the stack.depth() will be the number of events + 1 and will hold references to Items and Events.
Is this a bug or is there another way to clear the stack to remove all references to entities?
I can clear the session and that removes the JdbcValuesSourceProcessingState from the stack but I’d rather avoid that if possible.