hibernate二级缓存的配置

原文地址:http://ligson.iteye.com/blog/923959


1、在hibernate中配置
Xml代码   收藏代码
  1. <!-- 启用二级缓存 -->  
  2. <prop key="hibernate.cache.use_second_level_cache">true</prop>  
  3. <!-- 设置缓存提供者 -->  
  4. <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>  
  5. <!--使用查询缓存-->  
  6. <!--  
  7.     query.setCacheable(true);//激活查询缓存   
  8.     query.setCacheRegion("myCacheRegion");//指定要使用的cacheRegion,可选    
  9. -->  
  10. <prop key="hibernate.cache.use_query_cache">true</prop>  
 
2、在src下建立echcache.xml
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.  <ehcache>    
  3.      <diskStore path="java.io.tmpdir"/>    
  4.      <defaultCache    
  5.          maxElementsInMemory="10000"    
  6.          eternal="false"    
  7.          overflowToDisk="true"    
  8.          timeToIdleSeconds="120"    
  9.          timeToLiveSeconds="120"    
  10.          diskPersistent="false"    
  11.          />    
  12.  </ehcache>   
 
3、在类资源文件中加入
Xml代码   收藏代码
  1. <cache usage="read-only"/>  
  2.         <!--    
  3.         <cache usage="nonstrict-read-write"/>  
  4.         <cache usage="read-write"/>  
  5.         <cache usage="transactional"/>  
  6.         -->  
 
缓存策略
只读缓存(read-only):没有什么好说的
读/写缓存(read-write):程序可能要的更新数据
不严格的读/写缓存(nonstrict-read-write):需要更新数据,但是两个事务更新同一条记录的可能性很小,性能比读写缓存好
事务缓存(transactional):缓存支持事务,发生异常的时候,缓存也能够回滚,只支持jta环境,这个我没有怎么研究过

还可以在hibernate配置文件加入
<class-cache usage="read-only" class=""/>
--------------------------------------------------------------------------------
使用二级缓存的前提条件
------
你的hibernate程序对数据库有独占的写访问权,其他的进程更新了数据库,hibernate是不可能知道的。你操作数据库必需直接通过hibernate,如果你调用存储过程,或者自己使用jdbc更新数据库,hibernate也是不知道的。hibernate3.0的大批量更新和删除是不更新二级缓存的,但是据说3.1已经解决了这个问题。
这个限制相当的棘手,有时候hibernate做批量更新、删除很慢,但是你却不能自己写jdbc来优化,很郁闷吧。
SessionFactory也提供了移除缓存的方法,你一定要自己写一些JDBC的话,可以调用这些方法移除缓存,这些方法是:
Java代码   收藏代码
  1. void evict(Class persistentClass)   
  2. Evict all entries from the second-level cache.   
  3. void evict(Class persistentClass, Serializable id)   
  4. Evict an entry from the second-level cache.   
  5. void evictCollection(String roleName)   
  6. Evict all entries from the second-level cache.   
  7. void evictCollection(String roleName, Serializable id)   
  8. Evict an entry from the second-level cache.   
  9. void evictQueries()   
  10. Evict any query result sets cached in the default query cache region.   
  11. void evictQueries(String cacheRegion)   
  12. Evict any query result sets cached in the named query cache region.   
 
不过我不建议这样做,因为这样很难维护。比如你现在用JDBC批量更新了某个表,有3个查询缓存会用到这个表,用evictQueries(String cacheRegion)移除了3个查询缓存,然后用evict(Class persistentClass)移除了class缓存,看上去好像完整了。不过哪天你添加了一个相关查询缓存,可能会忘记更新这里的移除代码。如果你的jdbc代码到处都是,在你添加一个查询缓存的时候,还知道其他什么地方也要做相应的改动吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值