File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Relation between Hibernate Query Cache and Second Level Cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Relation between Hibernate Query Cache and Second Level Cache" Watch "Relation between Hibernate Query Cache and Second Level Cache" New topic
Author

Relation between Hibernate Query Cache and Second Level Cache

santosh joshi
Greenhorn

Joined: Aug 20, 2007
Posts: 21
I Need to implement ehcache in my existing project, for which i am using ehcache

  • WROTE ehcache.xml file

  • ENABLED cahcing in hibernate

  • ADDED entries in log4j for testing

  • I have and existing query like
    Now when i am running the above query in a loop for 10 times, it always hits the database and gets the result, the cache logs says "item already cached" also the SecondLevelCacheStatistics show 0 hits.
    Now when i added the setCacheable(true) to the below query
    The above worked and i got the expected result
    My question is that as i am working on existing maintaince project, do i need to add setCacheable(true) to every concerned query.
    Does second level caache depends on query cache (hibernate.cache.use_query_cache) ' value.

    I there any other way out so that i need not to write setCacheable(true) to every query?

    Thanks
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    A cached query is a different thing from cached entities. Remember, queries do not necessarily load entities (i.e. select property1 from entity will just get that property). When you cache a query what you cache are the ids of entities that match the criteria when the query was run. Because of this, there is no point using the query cache if you don't also use a second level cache, otherwise when you access an entity from the query results you'll hit the database again. So it only makes sense to explicitly define a query as cacheable, because you need to only cache those that access entities that may be in the second level cache.

    So yes, you need to define every query you want to be cacheable as such explicitly. Its worth noting that when it comes to performance changes to a Hibernate application, the query cache is the last performance change you would normally look at. If you've tried everything else and the application is still not performing well then you could look at the query cache, assuming your application's business logic is OK with out of date results. If you have not performance tested your application and are adding the query cache on a hunch, stop what you are doing. Typically there are far easier way to improve the performance of a Hibernate application.


    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    santosh joshi
    Greenhorn

    Joined: Aug 20, 2007
    Posts: 21
    I only want to cache some static read only tables like list of countries and every time this table gets hits the data should be read from the cache.

    Paul Sturrock wrote:A cached query is a different thing from cached entities. Remember, queries do not necessarily load entities (i.e. select property1 from entity will just get that property). When you cache a query what you cache are the ids of entities that match the criteria when the query was run.


    My understanding on your point is like

    A query wil hit the database every time it got fired but if the query is cacheble the DB hit is saved for the second and subsequent times .
    the query cache makes the query cachable but the secong level cache makes the Entities catchable based on their identifier(pk)

    The second level cache is only used when accessing the object by Primary Key, means the searching for whole entities based on Primary Key
    ie with session.get(), session.load() methods.

    Please correct me if i am wrong.
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    Yeah, that's exactly it.
    santosh joshi
    Greenhorn

    Joined: Aug 20, 2007
    Posts: 21

    Thanks Paul
    Paul Sturrock
    Bartender

    Joined: Apr 14, 2004
    Posts: 10336

    You are welcome
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Relation between Hibernate Query Cache and Second Level Cache