-->
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.  [ 6 posts ] 
Author Message
 Post subject: Confused between first level,query ,second level cache
PostPosted: Tue Jul 11, 2006 1:46 pm 
Newbie

Joined: Fri Apr 21, 2006 1:47 am
Posts: 6
I am too confused after reading lot of articles on hibernate caching.

Can anybody please help me in understanding the differences among the following:

1). First level cache
2). Query cache
3). Second level cache

Please don't ask me to read any article. I have already read so many and now confused.

Please help!!!

I have to apply caching in my project.


Top
 Profile  
 
 Post subject: Re: Confused between first level,query ,second level cache
PostPosted: Tue Jul 11, 2006 2:18 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
dsppHibernate wrote:
1). First level cache


The sometimes so called "first level cache" is also known as the Session object, that you retrieve from the SessionFactory, sometimes delegating this to the HibernateUtil helper class.

This object is always used in Hibernate. It's the basis. You use it do anything that might have to do with the persistance of your objects.

It is also called first-level cache, because for example, if you ask for an object of the same id twice or more in a short time, it will return the already retrieved object each time, issuing only one query for that.

Obviously, as always with a cache, there're some caveats to be aware of (synchro with db, mostly), so that's why there're some patterns to follow :
The most important is that the session should be short lived and only used for a use case context. Never think using only one session for a whole app session, for example !

Session is not threadsafe, sessionfactory is. So be careful when using sessions objects in a multi-threaded environment.

dsppHibernate wrote:
2). Query cache

You should read this, it is only about twenty lines of doc :
http://www.hibernate.org/hib_docs/v3/re ... querycache

As explained, you use it only when you've got queries that have to be executed many times. Obviously not always, because the required overhead to cache the results has to be compensated by the next call performance boost. If you cache queries that are only once or twice called, you see that you're going to get the contrary result of what you want : better perfs.

dsppHibernate wrote:
3). Second level cache

Second level is to be used for never-modified objects. So you configure this only for some classes (i.e. tables) of your system. It is designed for always accessed objects. So use second level cache only for entities that never change.

dsppHibernate wrote:
Please don't ask me to read any article. I have already read so many and now confused.

One advice : just practice, and then read the parts of the reference documentation when you need it. Don't try to read it in one shot, it's too big to do it. You won't be able to catch up everything at once.

But sorry, you're gonna have to read still a lot to be ok :-)

dsppHibernate wrote:
I have to apply caching in my project.

Well, what do you mean by this ? You have perfs problems ? If not, I do not understand why you're already thinking about caching. Optimization is always to be done after design, never before. :)

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 2:43 pm 
Newbie

Joined: Fri Apr 21, 2006 1:47 am
Posts: 6
Now another question arises here between query cache and second level cache:

1). Can I use second level cache without enabling query cache?

I am asking this because I have written a sample code and tried to use second level cache after enabling it in my cfg.xml file and disabled query cache,

but as per statistics my next call to get the same object is not hitting the cache. From last couple of days, I have tried so many things, but no clue.


2). Is the query cache also available among sessions. I mean whether this also available at session factory level like second level object caching.


Kindly reply


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 4:23 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Here's my understanding:

1). Can I use second level cache without enabling query cache?
Yes. I don't use query caching, but I most certainly use the second level cache. In fact, I don't think you can avoid it.

I am asking this because I have written a sample code and tried to use second level cache after enabling it in my cfg.xml file and disabled query cache, but as per statistics my next call to get the same object is not hitting the cache. From last couple of days, I have tried so many things, but no clue.

It depends on how you load the object as to whether or not it will used the cached version or not. For example, using a load() by ID will most certainly use the cache. Conversely some associations don't cache like you'd expect. For example, non-lazy collections won't have entities inside them cached. (I may be oversimplifying here... Corrections anybody?) Also, there are other rules as well. I heartily recommend reading the chapter on caching in Hibernate in Action.

2). Is the query cache also available among sessions. I mean whether this also available at session factory level like second level object caching.
I'm almost certain the query cache is stored in the second level cache mechanism. Check the source code. The documentation talks about cache regions and custom cache expiration policies. That stuff wouldn't make sense if the query cache were only on a per-session basis so it must be per SessionFactory.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 7:33 pm 
Newbie

Joined: Fri Apr 21, 2006 1:47 am
Posts: 6
Thanks a lot!!!. It clears most of my doubts.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 10:24 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Don't forget the ratings on answers, need those credits!


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