春季休眠ehcache设置 [英] Spring hibernate ehcache setup

查看:125
本文介绍了春季休眠ehcache设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题让hibernate二级缓存可用于缓存域对象。根据 ehcache文档 ,它不应该'要将缓存添加到我现有的工作应用程序中太复杂了。



我有以下设置(仅概述相关代码段):

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

ehcache-entity.xml

 < cache name =com.company.Entityeternal =false
maxElementsInMemory =10000overflowToDisk =truediskPersistent =false
timeToIdleSeconds =0timeToLiveSeconds =300
memoryStoreEvictionPolicy =LRU/>

ApplicationContext.xml

 < bean class =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean> 
< property name =dataSourceref =ds />
< property name =annotatedClasses>
< list>
< value> com.company.Entity< / value>
<列表>
< / property>
<适当的ty name =hibernateProperties>
<道具>
< 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依赖关系

 < dependency> 
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-annotations< / artifactId>
< version> 3.4.0.GA< / version>
< /依赖关系>

< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-hibernate3< / artifactId>
< version> 2.0.8< / version>
<排除项>
<排除>
< artifactId> hibernate< / artifactId>
< groupId> org.hibernate< / groupId>
< /排除>
< /排除>
< /依赖关系>

< dependency>
< groupId> net.sf.ehcache< / groupId>
< artifactId> ehcache-core< / artifactId>
< version> 2.3.2< / version>
< /依赖关系>

使用启用缓存统计信息的测试类:

 缓存缓存= cacheManager.getCache(com.company.Entity); 
cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
cache.setStatisticsEnabled(true);
//存储,读取等...
cache.getStatistics()。getMemoryStoreObjectCount(); //返回0

没有操作似乎触发任何缓存更改。我错过了什么?目前我在DAO中使用 HibernateTemplate ,这可能会有一些影响。
$ b



设置为DEBUG时,唯一的ehcache日志输出是:

  SettingsFactory:缓存区域工厂:net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory 

$ b

解决方案




  1. 正确的maven依赖关系:

     < dependency> 
    < groupId> org.hibernate< / groupId>
    < artifactId> hibernate-core< / artifactId>
    < version> 3.6.3.Final< / version>
    < /依赖关系>

    < dependency>
    < groupId> net.sf.ehcache< / groupId>
    < artifactId> ehcache-core< / artifactId>
    < version> 2.4.1< / version>
    < /依赖关系>


  2. 添加了 @Cacheable 注释从 javax.persistence 到我的实体。

  3. 从hibernate读取日志记录而不是ehcache。
    $ b

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

  4. 不是所有的hibernate操作都会影响缓存。这我需要进一步阅读。


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

解决方案

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.

这篇关于春季休眠ehcache设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆