[hibernate-dev] DefaultLoadEventListener.assembleCacheEntry several times slower than fetching state from 2nd level cache

Nikita Tovstoles nikita at doppelganger.com
Tue May 26 13:52:43 EDT 2009


I am about to file a bug but wanted to run this by the experts first.

Let's say there is a User entity class that declares 20 lazy collections much like so:

    @OneToMany( mappedBy = "owner")
    @Cascade({CascadeType.REMOVE, CascadeType.DELETE_ORPHAN})
    @LazyCollection(LazyCollectionOption.TRUE)
    @BatchSize(size = 1000)
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    public Set<CustomSpace> getCustomSpaces() 

In Hibernate 3.3.1.GA if I load ~1000 User entities from second level cache (w/o accessing any of the collections), I'll end up spending 5x time assembling cache entries vs. actually loading de-hydrated state from the cache provider (profiler snapshot attached). Most of the time is spent eagerly assembling CollectionTypes, all of which may or may not be needed during a given Session. 

So, basically, I'm being penalized for merely declaring OneToMany's. Currently, the only way to avoid this penalty - as far as I can tell - is to complicate the domain model by introducing some number of new entities that will be OneToOne to User; and redistributing collection declarations amongst those. But even that may not help: what if services using the domain model access a variety of collections at different times? IMO a much more scalable approach would be to:
* stash serialized states representing collection properties during assembleCacheEntry
* call CollectionType.assemble lazily when collection property field is actually accessed (a la lazy properties)

Fwiw, a post covering the above in a little more detail can be found here:
https://forum.hibernate.org/viewtopic.php?p=2412263#p2412263

-nikita


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: loadFromSecondLevelCache-profiled.txt
Url: http://lists.jboss.org/pipermail/hibernate-dev/attachments/20090526/491c4d1e/attachment.txt 


More information about the hibernate-dev mailing list