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.
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.
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.
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.
精彩评论