hibernate使用 Ehcache

来源:互联网 发布:gis地图数据 编辑:程序博客网 时间:2024/06/05 02:20
从hibernate2.1开始ehcache已经作为hibernate的默认缓存方案(二级缓存方案 sessionfactory级别), 在项目中有针对性的使用缓存将对性能的提升右很大的帮助。 

  要使用 Ehcache:需要一下步骤 

  一,classpath添加相应的jar(ehcache,commons-logging) 

  二,然后在hibernate.cfg.xml中配置 

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

  说明:如果没有配置<property name="cache.use_second_level_cache">true</property>(默认false) 将会产生根据单个id查询的情况(产生很多sql)。 

  三,为需要缓存的类添加缓存标示: 

  使用mapping文件时需要添加node : 

  Java代码   

@Entity  
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY) 

  如果使用使用hibernate annoation是使用@Cache(usage=CacheConcurrencyStrategy.)标签,有5种可选的缓存方案: 

  1,CacheConcurrencyStrategy.NONE 

  不适用,默认 

  2.  CacheConcurrencyStrategy.NONSTRICT_READ_WRITE 

  更新不频繁几个小时或更长 

  3,CacheConcurrencyStrategy.READ_ONLY 

  对于不发生改变的数据使用 [size=large][/size] 

  4,CacheConcurrencyStrategy.READ_WRITE 


  基于时间戳判定机制,,对于数据同步要求严格的情况,使用频繁 

  5,CacheConcurrencyStrategy.TRANSACTIONAL 

  运行在jta环境种,基于事务
0 0
原创粉丝点击