使用spring和hibernate配置ehcache和query cache

Hibernate ehcache and query cache 配置
环境: spring1.2.5+hibernate3.0.5
1、 applicationContext.xml
 
<prop key="hibernate.cache.provider_class">
                    org.hibernate.cache.EhCacheProvider
                </prop>
               
                <prop key="hibernate.cache.use_query_cache">
                    true
                </prop>
这两句加到 hibernateProperties中
< bean id = "hibernateTemplate"
        class = "org.springframework.orm.hibernate3.HibernateTemplate" >
        < property name = "sessionFactory" >
           < ref bean = "sessionFactory" />
        </ property >
        < property name = "cacheQueries" >
           < value > true </ value >
        </ property >
    </ bean >
添加此 bean到applicationcontext.xml中。在各个DAO的bean中,更改如下
< property name = "sessionFactory" >
           < ref bean = "sessionFactory" />
</ property >
改为
< property name = "hibernateTemplate" >
           < ref bean = "hibernateTemplate" />
</ property >
 
2、 ehcache.xml文件放在classes根目录即可
3、 pojo与ehcache.xml的配置关系
以 com.ce.ceblog.pojos.CeblogJournal为例子
在 CeblogJournal.hbm.xml中配置:
< class name = "CeblogJournal" table = "CEBLOG_JOURNAL" lazy = "false" >
        < cache usage = "read-write" region="ehcache.xml 中的 name 的属性值 " /> 注意:这一句需要紧跟在 class 标签下面,其他位置无效。
Ehcache.xml 文件主体如下
< defaultCache maxElementsInMemory = "10000" eternal = "false"
        timeToIdleSeconds = "1" timeToLiveSeconds = "1" overflowToDisk = "true" />
< cache name = "com.ce.ceblog.pojos.CeblogJournal" maxElementsInMemory = "10000"
        eternal = "false" timeToIdleSeconds = "300" timeToLiveSeconds = "600"
        overflowToDisk = "true" />
hbm文件查找cache方法名的策略:如果不指定hbm文件中的region= "ehcache.xml 中的 name 的属性值 " ,则使用 name 名为 com.ce.ceblog.pojos.CeblogJournal cache ,如果不存在与类名匹配的 cache 名称,则用 defaultCache
如果 CeblogJournal包含set集合,则需要另行指定其cache
例如 CeblogJournal包含ceblogReplySet集合,则需要
添加如下配置到 ehcache.xml中
< cache name = "com.ce.ceblog.pojos.CeblogJournal.ceblogReplySet"
        maxElementsInMemory = "10000" eternal = "false" timeToIdleSeconds = "300"
        timeToLiveSeconds = "600" overflowToDisk = "true" />
 
另,针对查询缓存的配置如下:
< cache name = "org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory = "5000"
        eternal = "true"
        overflowToDisk = "true" />
< cache name = "org.hibernate.cache.StandardQueryCache"
        maxElementsInMemory = "10000"
        eternal = "false"
         timeToLiveSeconds = "120"
        overflowToDisk = "true" />
4、 选择缓存策略依据:
<cache 
    usage="transactional|read-write|nonstrict-read-write|read-only" (1)
/>
ehcache不支持transactional,其他三种可以支持。
read-only:无需修改, 那么就可以对其进行 只读 缓存,注意,在此策略下,如果直接修改数据库,即使能够看到前台显示效果,但是将对象修改至cache中会报error,cache不会发生作用。另:删除记录会报错,因为不能在read-only模式的对象从cache中删除。
read-write:需要更新数据,那么使用 / 写缓存 比较合适,前提:数据库不可以为serializable transaction isolation level(序列化事务隔离级别)
nonstrict-read-write:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,那么比较适合使用 非严格读 / 写缓存策略。
5、 调试时候使用 log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。
6、 使用 ehcache,打印sql语句是正常的,因为query cache设置为true将会创建两个缓存区域:一个用于保存查询结果集(org.hibernate.cache.StandardQueryCache); 另一个则用于保存最近查询的一系列表的时间戳(org.hibernate.cache.UpdateTimestampsCache)。请注意:在查询缓存中,它并不缓存结果集中所包含的实体的确切状态;它只缓存这些实体的标识符属性的值、以及各值类型的结果。需要将打印sql语句与最近的cache内容相比较,将不同之处修改到cache中,所以查询缓存通常会和二级缓存一起使用。
小结:具体性能需要到服务器上进行测试,目前可以通过 log4j监控ehcache的变化过程。理解不当之处会在今后的工作中继续完善修改!谢谢!
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值