java 如何在不更改注释的情况下禁用 Hibernate 中某些实体的二级缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/934133/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:25:49  来源:igfitidea点击:

How I can disable the second-level cache of some certain entities in Hibernate without changing annotations

javahibernateannotationssecond-level-cache

提问by Kewei Shang

I'm using Hibernate second level cache in my application, for certain business reason I can't change the entity annotation any more.

我在我的应用程序中使用 Hibernate 二级缓存,出于某些商业原因,我不能再更改实体注释

In my project, apart from changing the Database from Hibernate, there exist also other native SQL that do not go through Hibernate. Therefore, the Hibernate second-level cache data could be stale after database being updated from native SQL. That's why I want to disable the second-level cache for certain entities (programmatically or other way than changing annotation).

在我的项目中,除了从 Hibernate 更改数据库之外,还存在其他不通过 Hibernate 的本机 SQL。因此,从原生 SQL 更新数据库后,Hibernate 二级缓存数据可能会过时。这就是为什么我想禁用某些实体的二级缓存(以编程方式或其他方式而不是更改注释)。

Thanks in advance!

提前致谢!

回答by Matej

WARNING:As Jens Schauder noted, it is impossible to configure Ehcache to store 0 elements in memory by setting maxElementsInMemory="0" as it effectively causes opposite effect - sets unlimited size for the cache. This behaviour is not mentioned on the Hibernate Cachingpage but is documented on Cache Configurationpage.

警告:正如 Jens Schauder 所指出的,不可能通过设置 maxElementsInMemory="0" 来配置 Ehcache 以在内存中存储 0 个元素,因为它实际上会导致相反的效果 - 为缓存设置无限大小。此行为未在Hibernate Caching页面上提及,但记录在Cache Configuration页面上。

I have quickly reviewed the documentation and haven't found alternative approach yet. I am unable to delete this answer by myself. :-(

我已经快速查看了文档,但还没有找到替代方法。我无法自己删除此答案。:-(

My original suggestion:

我原来的建议:

You can configure the implementation provider of second level cache to short TTL times and/or to store 0 entries of particular entity type.

您可以将二级缓存的实现提供程序配置为较短的 TTL 时间和/或存储特定实体类型的 0 个条目。

E.g. if you are using the Ehcache, you can configure it in ehcache.xml:*

例如,如果您正在使用 Ehcache,您可以在 ehcache.xml 中配置它:*

<cache
name="com.problematic.cache.EntityName"
maxElementsInMemory="0" <<== this should effectively disable caching for EntityName
overflowToDisk="false" <<== Do not overflow any entries to disk
/>

See Hibernate Cachingin Ehcache documentation.

请参阅Ehcache 文档中的Hibernate Caching

回答by Taylor Gautier

In Terracotta 3.1 and above, you can enable/disable Hibernate 2nd Level Caches on a per region basis, both in the configuration (statically) and at runtime, using the Terracotta Developer Console.

在 Terracotta 3.1 及更高版本中,您可以使用 Terracotta 开发人员控制台在配置(静态)和运行时按区域启用/禁用 Hibernate 2nd Level Caches。

You can also monitor in realtime statistics about the cache and Hibernate, for individual nodes in a cluster or cluster-wide.

您还可以针对集群中或集群范围内的各个节点实时监控有关缓存和 Hibernate 的统计信息。

Terracotta is open source. For more details, check out Terracotta for Hibernate.

兵马俑是开源的。有关更多详细信息,请查看Terracotta for Hibernate