0
votes

I've setup Hibernate to use EhCache for L2 caching of entities. Persistence.xml below:

<?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>

I've used the singleton cache provider so that I can get a copy of it in Spring, and expose the EhCache management objects via JMX:

<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> 

I'm exposing the Hibernate cache statistics using this method:

        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);

Finally, the 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>

Now, when I go an look at the statistics for both systems using JMX I get very different pictures. Note, I reset both sets of statistics and then waited ~30 seconds before taking these screenshots.

In the first screenshot is EhCache. Note the very small ObjectCount. This value fluctuates, but never gets higher than ~20 EhCache statistics

This is the Hibernate cache statistics. They look about right. Hibernate cache statistics

Why do the cache statistics report such wildly different things? It almost looks like the EhCache statistics are not working at all.

I'm using: - Hibernate 3.6.10 - EhCache 2.4.3 - Spring 3.2.4

1

1 Answers

1
votes

Same observation with the same error ;)

By default, statistics are disabled on EHCache ; you need to add statistics="true" to your cache definition in ehcache.xml.

Example :

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