[英]Why is Hibernate's cache statistics completely different to EhCache's statistics
我已經將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統計信息根本無法正常工作。
我正在使用:-Hibernate 3.6.10-EhCache 2.4.3-Spring 3.2.4
具有相同錯誤的相同觀察;)
默認情況下,EHCache上的統計信息是禁用的; 您需要在ehcache.xml中的緩存定義中添加statistics =“ true” 。
范例:
<cache name="global" maxElementsInMemory="10" eternal="true" overflowToDisk="false" statistics="true" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.