【问题标题】:Why is Hibernate's cache statistics completely different to EhCache's statistics为什么 Hibernate 缓存统计与 EhCache 统计完全不同
【发布时间】:2014-06-14 05:50:51
【问题描述】:

我已将 Hibernate 设置为使用 EhCache 进行实体的 L2 缓存。 Persistence.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="..." transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    ...
    <properties>
        <property name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" />
        <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />
        <property name="hibernate.cache.use_second_level_cache"
            value="true" />
        <property name="hibernate.cache.use_query_cache" value="false" />
        <property name="hibernate.generate_statistics" value="true"/>
        <property name="hibernate.connection.autocommit" value="false" />
    </properties>
</persistence-unit>
</persistence>

我使用了单例缓存提供程序,以便在 Spring 中获取它的副本,并通过 JMX 公开 EhCache 管理对象:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true" />
<bean id="managementService" class="net.sf.ehcache.management.ManagementService" 
     init-method="init"
     destroy-method="dispose">
    <constructor-arg ref="cacheManager"/>
    <constructor-arg ref="mbeanServer"/>
    <constructor-arg index="2" value="true"/>
    <constructor-arg index="3" value="true"/>
    <constructor-arg index="4" value="true"/>
    <constructor-arg index="5" value="true"/>
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
    <property name="locateExistingServerIfPossible" value="true"/>
</bean> 

我正在使用这种方法公开 Hibernate 缓存统计信息:

        MBeanServer mbs = 
                ManagementFactory.getPlatformMBeanServer();
        ObjectName mxbeanName = new ObjectName("Project:type=EmfCacheStatistics");
        StatisticsService statisticsService = new StatisticsService();
        statisticsService.setSessionFactory(((HibernateEntityManagerFactory)emf).getSessionFactory());
        statisticsService.setStatisticsEnabled(true);
        mbs.registerMBean(statisticsService, mxbeanName);

最后是ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect" dynamicConfig="true">

<diskStore path="java.io.tmpdir" />

<defaultCache maxElementsInMemory="10000" eternal="false"
    timeToIdleSeconds="300" timeToLiveSeconds="1800" overflowToDisk="true"
    diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU" />

</ehcache>

现在,当我查看使用 JMX 的两个系统的统计数据时,我得到了非常不同的图片。请注意,我重置了两组统计数据,然后等了大约 30 秒才截取这些屏幕截图。

在第一个屏幕截图中是 EhCache。请注意非常小的 ObjectCount。这个值会波动,但永远不会高于~20

这是 Hibernate 缓存统计信息。他们看起来差不多。

为什么缓存统计信息会报告如此截然不同的内容?几乎看起来 EhCache 统计数据根本不起作用。

我正在使用: - 休眠 3.6.10 - EhCache 2.4.3 - 春季 3.2.4

【问题讨论】:

    标签: hibernate jpa-2.0 ehcache


    【解决方案1】:

    同样的观察,同样的错误;)

    默认情况下,EHCache 上的统计信息是禁用的;您需要将 statistics="true" 添加到 ehcache.xml 中的缓存定义中。

    例子:

    <cache name="global" maxElementsInMemory="10" eternal="true" overflowToDisk="false" statistics="true" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-01
      • 2011-08-17
      • 2015-10-18
      • 2012-07-22
      • 1970-01-01
      • 2012-03-21
      • 2023-03-29
      • 2018-06-26
      相关资源
      最近更新 更多