【问题标题】:How to disable ehcache in persistence.xml file如何在 persistence.xml 文件中禁用 ehcache
【发布时间】:2011-12-03 12:57:53
【问题描述】:

所有, 是否可以在我的持久性 xml 文件中禁用二级缓存? 我有一个使用 ehcache 的 Spring+Hibernate+JPA 配置。在我的 persistence.xml 文件中,我有这个条目:

<property name="hibernate.cache.use_second_level_cache" value="false"/>

但这似乎不起作用,我仍然看到加载的实体数量不断增加,因为我的应用程序运行。我使用这个来获取统计信息:

EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
EntityManagerFactory emf = emfi.getNativeEntityManagerFactory();
EntityManagerFactoryImpl empImpl = (EntityManagerFactoryImpl)emf;
log.debug(empImpl.getSessionFactory().getStatistics());

请帮忙。

【问题讨论】:

  • 您在查看哪些统计信息?因为这个属性应该为所欲为,加载的实体数量与二级缓存无关。查看Statistics中所有以getSecondLevelCache开头的方法
  • 我正在查看以下统计信息:“entities loaded=30”
  • 加载的实体数是会话中加载的实体数,即一级缓存。

标签: hibernate spring web-applications ehcache second-level-cache


【解决方案1】:

您正在查看的统计信息是 Hibernate 会话缓存(第一级缓存)。您的二级缓存 (ehcache) 已禁用。您所体验的是正常行为。

编辑:

启用 ehcache 后,您会发现以下日志条目:

[#|...|INFO|sun-appserver2.1|org.hibernate.cfg.SettingsFactory|...|Second-level cache: enabled|#]
[#|...|INFO|sun-appserver2.1|org.hibernate.cfg.SettingsFactory|...|Cache provider: org.hibernate.cache.EhCacheProvider|#]

(前提是您为包启用日志记录 - 缓存提供程序可能会有所不同,例如可能是 net.sf.ehcache.hibernate.EhCacheRegionFactory - 不知道您使用的是什么)

【讨论】:

  • 是否有任何日志表明 ehcache 已禁用?如何确认 ehcache 实际上已禁用,如何找到从 ehcahce 提供的实体而不是休眠 1 级缓存?
  • @Ayusman:添加了有关 ehcache 日志的信息;正如 JB Nizet 建议的那样,您还可以查看从 empImpl.getSessionFactory().getStatistics() 获得的 Statistics 对象的 getSecondLevelCache*() 方法
猜你喜欢
  • 2021-05-09
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 2023-04-03
  • 2013-03-23
  • 2017-02-13
相关资源
最近更新 更多