首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate二级缓存的配备

2012-09-22 
hibernate二级缓存的配置1、在hibernate中配置!-- 启用二级缓存 --prop keyhibernate.cache.use_secon

hibernate二级缓存的配置

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

还可以在hibernate配置文件加入
<class-cache usage="read-only" name="code">void evict(Class persistentClass) Evict all entries from the second-level cache. void evict(Class persistentClass, Serializable id) Evict an entry from the second-level cache. void evictCollection(String roleName) Evict all entries from the second-level cache. void evictCollection(String roleName, Serializable id) Evict an entry from the second-level cache. void evictQueries() Evict any query result sets cached in the default query cache region. void evictQueries(String cacheRegion) Evict any query result sets cached in the named query cache region. ?
不过我不建议这样做,因为这样很难维护。比如你现在用JDBC批量更新了某个表,有3个查询缓存会用到这个表,用evictQueries(String cacheRegion)移除了3个查询缓存,然后用evict(Class persistentClass)移除了class缓存,看上去好像完整了。不过哪天你添加了一个相关查询缓存,可能会忘记更新这里的移除代码。如果你的jdbc代码到处都是,在你添加一个查询缓存的时候,还知道其他什么地方也要做相应的改动吗?

热点排行