-->
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.  [ 7 posts ] 
Author Message
 Post subject: Entity cache expire clears query cache too.
PostPosted: Fri Feb 26, 2010 4:20 pm 
Newbie

Joined: Fri Jan 09, 2009 12:52 pm
Posts: 6
I have two cache regions set up. One for the Employee entity and the other is for queries. I should say that my query results consist of lists of Employee objects. Problem is, when the entity cache times out, it also clears my query cache. It also does something strange to the query cache because it take 50 secs for the query to cache after the Employee entity timeout. Prior to any timeout, it only takes 7 secs to cache the first time. There has to be a way to control these seperately? I thought that was what the cache regions were for?


Top
 Profile  
 
 Post subject: Re: Entity cache expire clears query cache too.
PostPosted: Mon Mar 01, 2010 3:47 am 
Expert
Expert

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

you must be aware of the fact that Hiberante's 2L-Query-Cache only caches the primary key of the entities of the result set,
presuming that the entities itself are cached too in 2L cache.
If your entity cache times out before the 2L-Query-Cache does, then the 2L-Query-Cache will become counterproductive,
as Hibernate will then issue single sql's for retrieving the entities which aren't cached into entity 2L cache.
You may then have more database hit's than without 2L-Query-Cache enabled.
It is therefore important to configure the 2L-Cache in way that the Entity-2L-Cache is never evicted before Query-Cache does.


Top
 Profile  
 
 Post subject: Re: Entity cache expire clears query cache too.
PostPosted: Mon Mar 01, 2010 10:17 am 
Newbie

Joined: Fri Jan 09, 2009 12:52 pm
Posts: 6
I am aware of the points you made, however I'm not certain you answered my question. Ill try to deduce the answer from your response and maybe you can tell me if I'm right. So, it sounds like: The Entity cache is not seperate from the query cache? The ids cached for a query can only be cached if the Entity itself is cached? The fact that they are stored in different cache regions is just to confuse me? There is no answer to why it would suddenly take 50 seconds to cache the query that previously only took 5-6. I could also argue my case for wanting the entities to timeout ahead of the queries, but id rather get an answer for the above questions. Thanks!


Top
 Profile  
 
 Post subject: Re: Entity cache expire clears query cache too.
PostPosted: Mon Mar 01, 2010 12:00 pm 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
The Entity cache is not seperate from the query cache?


It depends how you configure/define your cache regions and the 2L-Cache itself.
By default they are stored in separate cache regions.

Quote:
The ids cached for a query can only be cached if the Entity itself is cached?

No, if fear you did'nt really understand what i meant. I try to explain it by example.

user-query1: select * from A
result-set query1 :
id = 1 name ="a"
id = 2 name ="b"
id = 3 name ="c"
id = 4 name ="d"

query result-set is cached now as something like following
{123123453,1,2,3,4} // the first entry is a timestamp, the others are primary key values

new transaction on a new session
user-query2: select * from A
query results cached !
if now the 4 instances are not cached anymore in the entity-region,
then hibernate will issue 4 separate queries in order to build up the result-set with entities
select * from A where id=1;
select * from A where id=2;
select * from A where id=3;
select * from A where id=4;

As you can see, it is feasible to cache a query into 2L-cache while not caching the entities of the concerning table,
but it is not recommended as instead to avoid 1 database hit you produce 4 database hits.


Top
 Profile  
 
 Post subject: Re: Entity cache expire clears query cache too.
PostPosted: Mon Mar 01, 2010 12:09 pm 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
It also does something strange to the query cache because it take 50 secs for the query to cache after the Employee entity timeout.


Do you mean the cache-put action takes 50 secs the second time?
Which 2L-cache implementation are you using?
Please issue some
jstack <pid>
during this 50 secs and report the stacks.
Then we should see where the bottleneck stays.


Top
 Profile  
 
 Post subject: Re: Entity cache expire clears query cache too.
PostPosted: Mon Mar 01, 2010 1:41 pm 
Newbie

Joined: Fri Jan 09, 2009 12:52 pm
Posts: 6
I think we are getting closer, but still un-answered. Example: I have a query set to eternal and the Entity set to timeout after 5 minutes. I start my app and login, which causes a single Entity to cache. I run the query. Query takes 5-6 seconds to run. I immediately run the same query again. It only takes 700 milliseconds(because it is cached). Everything to this point is expected behavior. I wait 5 minute and run my query. Query takes 50 seconds. Why is it all of the sudden did it take 50 seconds? It didn't take that long the first time I ran it. Why would it run single row select statements now? The two caches must be interacting in some way that has yet to be explained to me. If I run the query again right away, it takes 700 milliseconds. If I wait 5 minutes then it takes 50 seconds again. You've explained how query caching works, which I already knew, but why does the single Entity in a separate cache affect my query cache? To me, it should not. Thanks for stickin with me!


Top
 Profile  
 
 Post subject: Re: Entity cache expire clears query cache too.
PostPosted: Tue Mar 02, 2010 9:58 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Consider that also the database itself usually works with caches (and also this caches may expire).
So first I would try all without 2L-cache for regognizing how the database reacts to repeated + defered queries.

Quote:
Query takes 50 seconds.


I can only suggest you to perform some jstack during this 50 secs (maybee one each 5 secs) .
Than we can see effectivale what is happening.
Everything else is just something like guessing.

And again which 2L-cache implementation are you using?
(and which version of hibernate)


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