• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Relation between Hibernate Query Cache and Second Level Cache

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Bartender
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    santosh joshi
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah, that's exactly it.
     
    santosh joshi
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Thanks Paul
     
    Paul Sturrock
    Bartender
    Posts: 10336
    Hibernate Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are welcome
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic