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 Second level cache does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Second level cache does not work" Watch "Second level cache does not work" New topic
Author

Second level cache does not work

Alex Bokov
Greenhorn

Joined: Jun 01, 2009
Posts: 16
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.



Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Alex Bokov
Greenhorn

Joined: Jun 01, 2009
Posts: 16
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:
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Alex Bokov
Greenhorn

Joined: Jun 01, 2009
Posts: 16
query caching is working

but hibernate need to be kicked

with session factory

with entity manager
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Second level cache does not work
 
Similar Threads
EJB3 Entity Bean won't verify with Netbeans
EntityManager is null!! what can be the problem?
Trying to figure out how to use MySQL and generated @Id
JPA file accessed from different .ear files deployed on weblogic server
org. postgresql. util. PSQLException: ERROR: relation does not exist