1

I am just exploring on Ehcache self populating cache as hibernate second level cache.

Is hibernate support self populating cache?
Can anybody help me find few sample implementations/docs related to this topic?

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
VamsiKrishna
  • 197
  • 1
  • 2
  • 9

2 Answers2

1

VamsiKrishna, I'm not really sure what you try to achieve here, but I guess you want to avoid that two threads load the same entity from the DB. Now, I'm afraid you won't be able to achieve this. First, Hibernate doesn't store entities in the cache, but dehydrated representation of these. Creating these and then putting them so that the Ehcache layer can deal with them (they might rewrapped in some other internal types iirc), is all I believe not possible (Type accessibility would be one issue).

Also, you could use BlockingCache to mimic the behaviour and let Hibernate populate the Cache. The issue with that is that Hibernate won't put null values in the cache if I remember correctly. Not doing so would eventually lock your entire Cache.

Long story short, I think you're better off letting Hibernate deal with the concurrent access to the Cache (controlled by the cache access strategy) by itself, as nothing in there is really meant for a user to do...

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
Alex Snaps
  • 1,230
  • 10
  • 10
0

From ehcache website - http://www.ehcache.org/documentation/user-guide/hibernate#Configure-Ehcache-as-the-Second-Level-Cache-Provider

In hibernate.cfg.xml add

<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
Vivek
  • 1,316
  • 1
  • 13
  • 23
  • Hi Vivek, Thanks for your reply. The refrences you given were for regular ehcache as hibernate second level cache , but here I am looking for ** selfpopulating cache ** as second level cache. – VamsiKrishna Nov 22 '11 at 09:07