Hibernate version: 3.2.6
I'm using the open session in view pattern with long Conversations, and for my app its often necessary to use session.evict and session.clear to avoid unnecessary memory consumption.
In some cases we deal with persistence of big collections of entities with many relatioships on each entity. Hibernate is doing very well persisting this collections, but when we want to clear the session after a commit, profiling and debugging I see that the session.clear() command takes exponential time to do changes in all proxies on the session. Its the same with the evict() call, for all evict() called for an entity there is an interation on all proxies in the session to find the proxies of this entity.
To give an idea, my entity has 10 proxies. A collection with 30k entities generates 300k proxies in the session. I take less than 1 minute to do what I need and commit everything, but a session.clear() after this takes more than 30 minutes...
I know that is a problem to deal with large collections in session, but we've implemented many features with filters (like auditing, e.g.), that we'll loose doing bulk updates.
Any idea to clear the session in a more efficient way?
A relevant stack of a thread dump:
Code:
4XESTACKTRACE at java/util/AbstractMap.containsValue(AbstractMap.java:127)
4XESTACKTRACE at org/hibernate/engine/StatefulPersistenceContext.containsProxy(StatefulPersistenceContext.java:467)
4XESTACKTRACE at org/hibernate/proxy/AbstractLazyInitializer.isConnectedToSession(AbstractLazyInitializer.java:97(Compiled Code))
4XESTACKTRACE at org/hibernate/proxy/AbstractLazyInitializer.setSession(AbstractLazyInitializer.java:86(Compiled Code))
4XESTACKTRACE at org/hibernate/engine/StatefulPersistenceContext.clear(StatefulPersistenceContext.java:184(Compiled Code))
4XESTACKTRACE at org/hibernate/impl/SessionImpl.clear(SessionImpl.java:255)
The pice of code that does this linear search, in StatefulPersistenceContext is:
Code:
public boolean containsProxy(Object entity) {
return proxiesByKey.containsValue( entity );
}