java 休眠禁用缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6830618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate disabling cache
提问by AAaa
I want to disable hibernate caching.
我想禁用休眠缓存。
session.setCacheMode(CacheMode.IGNORE)
doesn't work. query.setCacheable(false)
also doesn't work.
session.setCacheMode(CacheMode.IGNORE)
不起作用。query.setCacheable(false)
也不起作用。
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?
另外,我能否以某种方式配置缓存不会对对象 X、Y 进行,但会为对象 Z 进行?
Thanks.
谢谢。
采纳答案by AAaa
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.
我在网上搜索过,结论是确实无法禁用会话缓存。这很奇怪。我自己的解决方案是在我想要清除缓存时使用 session.clear()。
回答by Valdas Rap?evi?ius
You can call session.clear()
just before obtaining reusable session object from the manager.
您可以session.clear()
在从管理器获取可重用会话对象之前调用。
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 SessionManagerthat stores all sessions and delivers on demand. Just before delivering call session.clear()
and this solves the problem.
即在我们的项目中,我们必须在许多休眠会话(每个 http 会话一个)之间同步更新。二级缓存工作正常,但必须禁用或清除一级(每个会话)。因此,我们创建了SessionManager来存储所有会话并按需交付。就在传递电话之前session.clear()
,这解决了问题。
Until you're doing your unit of work - 1st level cache is fine.
在你完成你的工作单元之前 - 一级缓存很好。
回答by Claude Houle
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)
但是请注意,您不能禁用 Hibernate 提供的会话缓存(如果不是所有 JPA 实现)。如果你真的不想被 Hibernate 缓存,你可以驱逐它或不使用 hibernate(以对你来说更简单的方式)
As for caching specific entities, I recommend you take a look at javax.persistence.Cacheable and see how that works for you.
至于缓存特定实体,我建议您查看 javax.persistence.Cacheable 并了解它如何为您工作。