【问题标题】:How to clear / refresh the cache in EclipseLink如何清除/刷新 EclipseLink 中的缓存
【发布时间】:2017-07-04 12:04:59
【问题描述】:

我在我的 Web 应用程序的 persistence.xml 中使用以下设置(我使用 Eclipselink 2.6.4):

<properties> 
    <property name="eclipselink.jdbc.cache-statements" value="true" />        
    <property name="eclipselink.cache.query-results" value="true" /> 
    <property name="eclipselink.ddl-generation.index-foreign-keys" value="true" />
    <property name="eclipselink.logging.level" value="OFF" />        
    <property name="eclipselink.persistence-context.close-on-commit" value="true" />        
    <property name="eclipselink.persistence-context.flush-mode" value="commit" />        
    <property name="eclipselink.persistence-context.persist-on-commit" value="false" />
</properties>

我正在使用@Cacheable(false) 注释来防止某些实体被缓存。
@Cache 注释在 2.6.4 版本中不再起作用。
我的问题是,是否有可能在全球范围内清除缓存?让每3小时说一次?
谢谢

【问题讨论】:

  • 为什么缓存注解不起作用?有关使缓存失效,请参阅 wiki.eclipse.org/EclipseLink/UserGuide/JPA/…eclipse.org/eclipselink/documentation/2.5/concepts/cache006.htm
  • 注释不起作用的原因与我的IDE(netbeans)有关。导入包javax.persistence.Cache 我得到一个不兼容的类型错误。我必须导入正确的包(org.eclipse.persistence.annotations)。无论如何,缓存注释不是我正在寻找的解决方案。我需要一个全球性的。有没有?谢谢
  • 全局无效是什么意思,仅适用于共享缓存,在 EMF 级别。如果您有多个应用程序在运行,则可以查看缓存协调,这将允许每个应用程序侦听事件。见eclipse.org/eclipselink/documentation/2.6/concepts/cache010.htm
  • 我明白,所以我必须更改每个实体,或者最好让它们都扩展一个超类,在其中定义将被继承的缓存策略。该文件非常有趣。我需要一些时间来深入了解。谢谢克里斯。

标签: java eclipselink


【解决方案1】:

一级缓存默认开启,不能关闭。即,您的 persistence.xml 文件中的任何设置都不会禁用一级缓存。

您只能通过调用清除所有实体管理器对象

entityManager.clear()

这将使后续查询(第一次)进入数据库,然后对象再次存储在缓存中

在您的情况下,您需要将 entitymanager 对象存储在某处的寄存器中,并每 3 小时循环一次以调用 clear() 函数。

您可以通过调用强制每个查询直接进入数据库

query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 2011-03-14
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多