3

I want to disable hibernate caching.

session.setCacheMode(CacheMode.IGNORE) doesn't work.
query.setCacheable(false) also doesn't work.

In addition, can I configure in some way that caching won't be done for Objects X,Y but will be done for Object Z?

Thanks.

2
  • 1
    Can you be more specific about which hibernate cache you are referring to? There is the session/first-level cache, second-level cache, query cache, and collection cache. Commented Jul 26, 2011 at 13:44
  • i'm referring to session cache Commented Jul 27, 2011 at 12:34

4 Answers 4

7

You can call session.clear() just before obtaining reusable session object from the manager.

I.e. in our project we had to synchronize updates between many hibernate sessions (one per http session). 2nd level cache works fine but 1st level (per session) had to be disabled or cleared. Thus we created SessionManager that stores all sessions and delivers on demand. Just before delivering call session.clear() and this solves the problem.

Until you're doing your unit of work - 1st level cache is fine.

Sign up to request clarification or add additional context in comments.

Comments

3

try something like that when configuring your hibernate session factory:

configuration.setProperty( Environment.USE_QUERY_CACHE, Boolean.FALSE.toString() );
configuration.setProperty( Environment.USE_SECOND_LEVEL_CACHE, Boolean.FALSE.toString() );
configuration.setProperty(Environment.CACHE_REGION_FACTORY,org.hibernate.cache.impl.NoCachingRegionFactory.class.getName());
configuration.setProperty(Environment.CACHE_PROVIDER,org.hibernate.cache.NoCacheProvider.class.getName());

However note that you cannot disable the Session Cache which is provided by Hibernate (If not all JPA implementations). If you really something not to be cached by Hibernate, you can either evict it OR not use hibernate (whichever is simpler to you)

As for caching specific entities, I recommend you take a look at javax.persistence.Cacheable and see how that works for you.

2 Comments

are you sure there is no way to disable session cache? seems strange.. what if i want to comapre DB object before and after change? is the only wasy is it use evict?
I'm pretty sure... When you are dealing with Hibernate Entities, you are dealing with <attached> objects to a session. Invoking Session.get() on said entities should return you the same object handle (or a proxy of said entities)
1

I've searched the web and the conclusion is that indeed a session cache can't be disabled. This is very odd. My solution for myself is to use session.clear() when i want the cache to be cleared.

2 Comments

doesn't work, I already tried session.clear(); still direct db updates doesn't show up in hibernate. please let me know if you had figured out something else.
Work for me. You can try also to close and open new session.
0

In case anyone stumbles on this searching for a solution, this is what worked for me:

instead of session.setCacheMode(CacheMode.IGNORE) I used session.setProperty(AvailableSettings.JPA_SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS) which did the trick for me to let hibernate bypass the second level cache on the next entity load call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.