AAaa
AAaa

Reputation: 3819

hibernate disabling cache

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.

Upvotes: 3

Views: 12941

Answers (4)

Lukas Hinsch
Lukas Hinsch

Reputation: 1860

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.

Upvotes: 0

Valdas Rapševičius
Valdas Rapševičius

Reputation: 416

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.

Upvotes: 7

AAaa
AAaa

Reputation: 3819

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.

Upvotes: 1

Claude Houle
Claude Houle

Reputation: 42856

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.

Upvotes: 3

Related Questions