-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I Modify This Query So That It Results In A Cache Hit?
PostPosted: Thu May 26, 2005 1:58 pm 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
I have a collection that does not change (several actually, but in this example a list of TimeZones), which I have placed as immutable, read-only members of the second-level cache using ehcache. Once this Collection is in memory, there should be no reason to go to the database to load it again. That is not what I am seeing, however, and I am wondering if there is a way to modify what I am doing so that the findAll method returns the cached results or if this is simply not supported. I can see the complexities involved with concurrent modifications, but it seems that in the case of an immutable, read-only cache, it *might* be possible to support it.

As an asside, if I do the second test twice and have two instances of the default TimeZone in memory, I am surprised the assertSame(zoneOne, zoneTwo) fails.

Perhaps there is a URL to some additional documentation of ehcache that would help?
Hibernate version:

3.0.3

Mapping documents:
TimeZone is not mutable and the cache is read-only

Code Excerpts:


Code:
public static Collection findAllTimeZones() {
        return HibernateUtil.currentSession().createCriteria(TimeZone.class).list();
}


Unit tests:
Code:
public void testCache() throws Exception {
        CacheProviderHelper.dumpCacheInfo();
        /* first read all TimeZones into memory (and thus into the cache) */
        Collection allZones = BankManager.findAllTimeZones();
        int initialHitCount = CacheProviderHelper.getHitCount();
        HibernateUtil.closeSession();
       
        /* should hit second-level cache  */
        TimeZone zoneOne = BankManager.getDefaultTimeZone();
        int currentHitCount = CacheProviderHelper.getHitCount();
        assertEquals(initialHitCount + 1, currentHitCount);
        HibernateUtil.closeSession();
       
        initialHitCount = currentHitCount;
        allZones = BankManager.findAllTimeZones();
        currentHitCount = CacheProviderHelper.getHitCount();
        /* I am surprised this isn't a hit.  I guess there is no way for the caching machinery to know that the cache is 100% complete. */
        //assertEquals(initialHitCount + allZones.size(), currentHitCount);
        assertEquals(initialHitCount, currentHitCount);
        HibernateUtil.closeSession();
       
        /* here these should all be cache hits. */
        for (Iterator i = allZones.iterator(); i.hasNext();) {
            HibernateUtil.currentSession().get(TimeZone.class, ((TimeZone)i.next()).getTimeZoneId());
        }
        currentHitCount = CacheProviderHelper.getHitCount();
        assertEquals(initialHitCount + allZones.size(), currentHitCount);
        HibernateUtil.closeSession();
  }



Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 8:14 pm 
Regular
Regular

Joined: Tue May 24, 2005 10:19 am
Posts: 65
you can store in collection... I use:
Code:
{
class preloaddata
Collection timezones;
public Collection getTimeZones() {
        timezones HibernateUtil.currentSession().createCriteria(TimeZone.class).list();
        return  timezones ;
} }

this manner is idepdent of cache and others things.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 9:31 am 
Regular
Regular

Joined: Tue May 24, 2005 10:19 am
Posts: 65
... the correct code...

{
class preloaddata
Collection timezones=null;
public Collection getTimeZones() {
if (timezones==null )
timezones HibernateUtil.currentSession().createCriteria(TimeZone.class).list();
return timezones ;
} }


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 10:32 am 
Regular
Regular

Joined: Wed Nov 17, 2004 11:49 am
Posts: 65
Location: Pittsburgh
True. My only problem with that is that it isn't using the second-level cache and doubles (potentially) the number of objects in memory. It's not optimal, but the collection is small enough that I am willing to do it if there isn't any other way.


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

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.