-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Collection cache stupidity in Hibernate Core
PostPosted: Sat Mar 29, 2008 8:56 pm 
Newbie

Joined: Wed Aug 27, 2003 7:31 am
Posts: 9
Location: Russia, Izhevsk
Currently, 2-nd level collection caching system is STUPID.

Let's look into this method of the class PersistentList:
Code:
   public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner)
   throws HibernateException {
      Serializable[] array = ( Serializable[] ) disassembled;
      int size = array.length;
      beforeInitialize( persister, size );
      for ( int i = 0; i < size; i++ ) {
         list.add( persister.getElementType().assemble( array[i], getSession(), owner ) );
      }
   }

This is a very BAD idea.

Let me explain. First of all, collection caches contain only object IDs, not the objects themselves.

That means collections and their elements are cached separately. And it's definitely possible, that most of collection elements will be pushed out of the cache.

And in this case Type.assemble(...) will issue "select * from blah where blah.id=?" FOR EACH COLLECTION ELEMENT NOT IN THE 2nd-level CACHE. Of course, it's SLOW.

The possible solution will be something like (pseudocode):
Code:
   public void initializeFromCache(CollectionPersister persister, Serializable disassembled, Object owner)
   throws HibernateException {
      Object[] ids = ( Object[] ) disassembled;
      int size = ids.length;
      beforeInitialize( persister, size );
               
                List res=getSession().createCriteria(
                       persister.getElementType().getEntityName()).
                       .add(Restriction.in("id",ids)
                       .list();
                //Add loading in chunks, etc.

                list.addAll(res);
   }


Is there any pitfall I can't see?

_________________
Sapienti sat!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.