-->
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.  [ 12 posts ] 
Author Message
 Post subject: Hibernate cache L1 and L2
PostPosted: Mon May 21, 2012 11:16 am 
Newbie

Joined: Mon May 21, 2012 10:53 am
Posts: 6
Hi I was looking into this posts about hibernate cache mechanism:

http://www.javalobby.org/java/forums/t48846.html
http://www.javacodegeeks.com/2012/02/hi ... orial.html

In the session cache (L1) it caches objects and in factory cache (L2) it caches query results but not objects?
I don't understand if hibernate, despite the query results cached in L2 it also caches the serialized object there.
How does hibernate reconstructs an object that is already in the query results cache? Does it have to rebuild the object from the query cached data or it also has cached the serialized object? (I belive that using a serialized object might perform better)
Can someone provide me some documentation or explanation about hibernate caching L1 and L2?

thanks it advanced


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Tue May 22, 2012 2:34 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
The L1 caches entity objects and persistent collections (since Hibernate4.1 also natural-id's)
The L2 can cache:
-entity objects
-persistent collections
-query results
-natural id's (since Hibernate4.1)

Important to note is, that the query result cache memorizes only the primary-keys of the entity objects,
assuming that the rest of the data can be found in the entity 2L cache.
Therefore: Applying 2L-query-cache for a certain query makes only sense,
if also concerning entity class is declared for 2L-caching.

Documentation can be found here:
http://docs.jboss.org/hibernate/orm/4.1 ... ance-cache


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Tue May 22, 2012 10:16 am 
Newbie

Joined: Mon May 21, 2012 10:53 am
Posts: 6
Thanks for replying!
And what may happen under this condition?
Suppouse I have entity "Movie", let's also suppouse I have activated all caching mechanisms for L1 and L2 (query results, collections, entities).
So I start my app and make a query like:
"from Movie m where m.title like '%Predator%'" returns de data for 3 "predator" movies.
So now hibernate has cached the entities for the 3 predator movies in L2 and the querie results.

What happens if now I add a new movie?
Movie movie = new Movie();
movie.setTitle( "Rambo" );
entityManager.persist( movie );

Does this save action makes all the previously cached movies data to become invalidated?
How does the cache invalidation mechanism work?


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Tue May 22, 2012 10:46 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Does this save action makes all the previously cached movies data to become invalidated?


No. But the cached-query-result will become invalidated.

Quote:
How does the cache invalidation mechanism work?


Doing the insert on "Rambo" in Movies, hibernate will update UpdateTimestampsCache.
UpdateTimestampsCache expressed in poor words is a cached table with columns tablename and lastUpdate.
Doing the insert on "Rambo" in Movies, hibernate will update UpdateTimestampsCache by writing values
"Movies"|<timestamp> in the cached table.
When you then re-execute the cached query, hibernate recognizes that the cached result-set is older
than concerning timestamp in UpdateTimestampsCache (hibernate checks up-to-dateness of all query-involved tables),
thus the cached result-set get invalidated.


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Tue May 22, 2012 2:02 pm 
Newbie

Joined: Mon May 21, 2012 10:53 am
Posts: 6
So when query results are invalidated also are the entity objects? all must be regenerated?
Could you recommend me some book or lecture about this topic?

thanks a lot again


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Wed May 23, 2012 3:31 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
So when query results are invalidated also are the entity objects?

No, the entity objects get not invalidated (evicted), when the query result get invalidated.
There is no reason for doing that.
Quote:
Could you recommend me some book or lecture about this topic?


see the reference to Christans/Gavins book at the top of the page.
Book "Java persistence with Hiberante" second edition from Manning.
It's rather old (2006), but there the archicture of 2L-cache is explained a little more deep than in the online documentation
and finally the architecture is still the same (except that since Hibernate4.1 there's a dedicated region for natural-id cache)


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Wed May 23, 2012 9:36 am 
Newbie

Joined: Mon May 21, 2012 10:53 am
Posts: 6
Ok pb00067,
thanks a lot for your explanations, they have been very helpful.
best regards.


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Wed May 23, 2012 1:14 pm 
Newbie

Joined: Mon May 21, 2012 10:53 am
Posts: 6
Sorry to bother again, but can't figure this out from docs..

How should I configure 2nd level provider class for hibernate 4?
Should I set hibernate.cache.provider_class or hibernate.cache.region.factory_class property?

When doing excecuting this:
Map cacheEntries = sessionFactory.getStatistics()
.getSecondLevelCacheStatistics("events")
.getEntries();

I'm getting:
org.hibernate.cache.spi.QueryKey cannot be cast to org.hibernate.cache.spi.CacheKey
Do you have any recommendation on regarding debugging cache contents and it's statistics?


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Thu May 24, 2012 2:14 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
In Hibernate4 I use
Code:
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Thu May 24, 2012 2:51 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I'm getting:
org.hibernate.cache.spi.QueryKey cannot be cast to org.hibernate.cache.spi.CacheKey
Do you have any recommendation on regarding debugging cache contents and it's statistics?


Can you please post the entire stacktrace of the exception?
I guess this a configuration issue.
How do you set hibernate.cache.region.factory_class parameter and which Hibernate version are you using exactly?


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Thu May 24, 2012 9:39 am 
Newbie

Joined: Mon May 21, 2012 10:53 am
Posts: 6
My cache configuration:
<property name="hibernate.cache.provider_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>


the full trace:
Exception in thread "main" java.lang.ClassCastException: org.hibernate.cache.spi.QueryKey cannot be cast to org.hibernate.cache.spi.CacheKey
at org.hibernate.stat.internal.ConcurrentSecondLevelCacheStatisticsImpl.getEntries(ConcurrentSecondLevelCacheStatisticsImpl.java:80)
at test.hb4.App.performTest(App.java:121)
at test.hb4.App.main(App.java:30)

ps: I'm reading the book "Java Persistence with Hibernate" you recommended me, it's great.
:)


Top
 Profile  
 
 Post subject: Re: Hibernate cache L1 and L2
PostPosted: Fri May 25, 2012 2:10 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi Polaco,

now I was able to reproduce the ClassCastException.
It looks like there's a little bug in method SecondLevelCacheStatistics#getEntries
as it works only for non-query-cache regions like ClassCache- or CollectionCache-regions.

Anyway you can check the statics in the query-cache-region by
Code:
getSessionFactory().getStatistics().getQueryCacheHitCount());
getSessionFactory().getStatistics().getQueryCachePutCount());


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.