Hibernate Caching Story

Hibernate Version: 3.2.6.ga

Update respective files as follows.

ClassName1.hbm.xml




        sql query ...
    

asdf-Hibernate.cfg.xml


    org.hibernate.cache.EhCacheProvider


    /META-INF/hibernate/abcd-ehcache.xml

true
true

asdf-ehcache.xml


    
    

    <!-- Default cache region settings -->
    

    &lt;!-- If we didn&#039;t enable this, default settings will be applied.  
        
        --&gt;


Test Case 1:

To test Query Level Cache use for loop and query using named query.

In log you should see
DEBUG [main] (StandardQueryCache.java:117) – returning cached query results

Test Case 2:

To test Second Level Cache
Use for loop and get object using primary key

Verification Step 1:

Debug window Expression
net.sf.ehcache.CacheManager.ALL_CACHE_MANAGERS

If you open ehcache node, you should see all objects.
During debug, hitcount should be increased.

Verification Step 2:


    


    

You should see clear logs in console. Telling that after first hit onwards it is returning from cache.

DEBUG [main] (ReadWriteCache.java:85) – Cache hit: com.abc.ClassName1#1
DEBUG [main] (ReadWriteCache.java:75) – Cache lookup: com.abc.ClassName1#1
DEBUG [main] (EhCache.java:68) – key: com.abc.ClassName1#1
DEBUG [main] (MemoryStore.java:135) – com.abc.ClassName1Cache: com.abc.ClassName1MemoryStore hit for com.abc.ClassName1#1
DEBUG [main] (ReadWriteCache.java:85) – Cache hit: com.abc.ClassName1#1

Links to study:

http://ehcache.sourceforge.net/documentation/configuration.html
http://ehcache.sourceforge.net/documentation/hibernate.html
http://www.hibernate.org/hib_docs/reference/en/html_single/#performance-cache

Story:

It worked few times and didn’t work few times. Struggled to resolve this.

Reason: Temp files are locked in windows explorer. Close Eclipse. Clear all temp files in temp folder. After fresh restart, it worked fine for me.

Tips:

If system memory is limited and if you define 10000 records to be cached, it will cause slow performance.

Need to be careful with read-only and read-write cache attributes

Define realistic timeToLiveSeconds. Dont give infinite numbers

Read more on each attribute and think on it.

This configuration didn’t worked out


    net.sf.ehcache.hibernate.EhCacheProvider


    /META-INF/hibernate/asdf-ehcache.xml

true
true
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s