Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Hibernate]: Second Lv Cache (with Async Updates)

 
Terry Ong
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers,

I have this peculiar requirement to cache objects in an application.
These objects cached are data mainly used for gui-construction.
The problem here is that these data can be updated by the same application, but the changes in the database should not effect till the user forces a refresh on the cache.

Currently, I've tried to do this using two HibernateUtils with two sessionfactories (reading from different hibernate.cfg.xml but using same entities). For the hbms in my HibernateUtils2, i've declared the entity to be read-only.

However, when I updated the object from SessionFactory1, the object cached in SessionFactory2 is also being updated.

The way I can do what I want now is by using 2 separate process.


Any idea, where i've gone wrong? (sorry i'm kinda new to all these orm and especially caching)

Thanks in advance.

Regards
Terry.
 
Lakshmi Dasari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather strange requirement. Are you having cache definitions in both the configuration files ? And are you sure the cache is being hit when using SessionFactory2 and not the database?
 
Terry Ong
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Rather strange requirement.

Yes, I agree.

> Are you having cache definitions in both the configuration files ?

Yes for both of them

[hibernate1.cfg]
GuiData entity -> GUI_TABLE Tablename
CacheGuiData entity -> GUI_TABLE Tablename

[hibernate2.cfg]
CacheGuiData entity -> GUI_TABLE Tablename


> And are you sure the cache is being hit when using SessionFactory2
> and not the database?


I've turn on the statistics for 2nd level cache using the follow code;
and there is some hits:
long hits = sf.getStatistics().getSecondLevelCacheHitCount();

is this correct?
 
Terry Ong
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I found the error.
I declared the same ehcache config for both hibernate.cfg.xml.
Somehow due to this the entities get interference from each other.


Thanks for the tip, Lakshmi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic