Spring hibernate ehcache setup

此生再无相见时 提交于 2019-12-22 12:26:36

问题


I have some problems getting the hibernate second level cache to work for caching domain objects. According to the ehcache documentation it shouldn't be too complicated to add caching to my existing working application.

I have the following setup (only relevant snippets are outlined):

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE
public void Entity {
    // ... 
}

ehcache-entity.xml

<cache name="com.company.Entity" eternal="false"
    maxElementsInMemory="10000" overflowToDisk="true" diskPersistent="false"
    timeToIdleSeconds="0" timeToLiveSeconds="300"
    memoryStoreEvictionPolicy="LRU" />

ApplicationContext.xml

<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="ds" />
    <property name="annotatedClasses">
        <list>
            <value>com.company.Entity</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="net.sf.ehcache.configurationResourceName">/ehcache-entity.xml</prop>
            <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
            .... 
    </property>
</bean>

Maven dependencies

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-hibernate3</artifactId>
        <version>2.0.8</version>
        <exclusions>
            <exclusion>
                <artifactId>hibernate</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.3.2</version>
    </dependency>

A test class is used which enables cache statistics:

    Cache cache = cacheManager.getCache("com.company.Entity");
    cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
    cache.setStatisticsEnabled(true);
    // store, read etc ... 
    cache.getStatistics().getMemoryStoreObjectCount(); // returns 0

No operation seems to trigger any cache changes. What am I missing? Currently I'm using HibernateTemplate in the DAO, perhaps that has some impact.

[EDIT]

The only ehcache log output when set to DEBUG is:

SettingsFactory: Cache region factory : net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory

回答1:


Do you need to manually tell Hibernate to use the EHCache provider? I've never really been sure if this is required, but Hibernate does support a number of cache providers so I suspect that it might be necessary to explicitly tell Hibernate which one you want. Try adding this property to ApplicationContext.xml:

<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>



回答2:


There were several causes that I've identified:

  1. Correct maven dependencies:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    
  2. Added the @Cacheable annotation from javax.persistence to my entities.

  3. Read logging from hibernate instead of ehcache.

    getSessionFactory().getStatistics().logSummary();

  4. Not all hibernate operations seems to affect the cache. This I need to read up on further.




回答3:


By looking at your config, it all seems fine afaict. Only thing worth noticing, is that when using the HibernateTemplate, you have to explicitly setCacheQueries(true) if you plan on using the query cache... Which I wouldn't recommend, except if you really need it: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/HibernateTemplate.html#setCacheQueries(boolean)

Did you try the Hibernate statistics instead of the Ehcache ones ? Do you get cache misses there ? (reason I ask is to be sure you do use the same CacheManager as Hibernate does)...




回答4:


you can reference following configure

<prop key="hibernate.cache.use_query_cache">true</prop>



回答5:


In hibernate.cfg.xml add:

 <hibernate-configuration>
   <session-factory>
     ...
     <property name="cache.use_second_level_cache">true</property>
     <property name="cache.use_query_cache">true</property>
     <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>


来源:https://stackoverflow.com/questions/5364752/spring-hibernate-ehcache-setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!