Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Second level cache does not work

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to make work hibernate second level caching not successfully.

I have:





persistence.xml


CategoryServiceImpl.java



and every time I call findAll() in CategoryService it calls data base, at least I see query log.

Is there some thing that I missed?
How to get caching work?

Thanks for any help.



 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only certain method hit the cache. From the documentation:


Whenever you pass an object to save(), update() or saveOrUpdate() and whenever you retrieve an object using load(), get(), list(), iterate() or scroll(), that object is added to the internal cache of the Session.


 
Alex Bokov
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that caching works for single element

with session factory:
and with entity manager:

but does not work for lists

with session factory:
and entity manager:
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read Paul's post again.

Anytime you run a query whether from Criteria, HQL, or SQL Hibernate will NOT get data from the second level cache.

But if you turn on the second level query cache, then some queries can go to the query cache. In the query cache only IDs are stored. And there is a requirement that the Objects those IDs represent must be in the standard second level cache in a cache region.

One issue you have with the query cache is that the resultset could get stale real quickly if data is inserted or deleted from the database.

Mark
 
Alex Bokov
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
query caching is working

but hibernate need to be kicked

with session factory

with entity manager
 
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic