`
xiaofengtoo
  • 浏览: 485327 次
  • 性别: Icon_minigender_1
  • 来自: xiamen
社区版块
存档分类
最新评论

Hibernate使用EHcache二级缓存

阅读更多

引用:http://www.cnblogs.com/jianxh/articles/696399.html   blue@cnblogs的部落格

 

hibernate的session提供了一级缓存,每个session,对同一个id进行两次load,不会发送两条sql给数据库,但是session关闭的时候,一级缓存就失效了。

  二级缓存是SessionFactory级别的全局缓存,它底下可以使用不同的缓存类库,比如ehcache、oscache等,需要设置hibernate.cache.provider_class,我们用ehcache。
  缓存可以简单的看成一个Map,通过key在缓存里面找value。

配置:
Ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
 <diskStore path="硬盘目录"/>
  <defaultCache
   maxElementsInMemory="10000" <!-- 缓存最大数目 -->
   eternal="false" <!-- 缓存是否持久 -->
   overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时-->
   timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->
   timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->
   diskPersistent="false"
   diskExpiryThreadIntervalSeconds= "120"/>
</ehcache>

defaultCache为默认的缓存策略,可以根据各个不同的实例单独制订缓存策略
如:
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true"/>

Hibernate配置文件中设置
如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置
hibernate.cache.use_query_cache true 才行
<prop key="hibernate.cache.use_query_cache">true</prop>

<!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->
<prop key="hibernate.cache.provider_class">
org.hibernat.cache.EhCacheProvider
</prop>

<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="cacheQueries">
<value>true</value>
</property>
</bean>

Hbm
<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:只偶尔需要更新数据(也就是说,两个事务同时更新同一记录的情况很不常见),也不需要十分严格的事务隔离,那么比较适合使用非严格读/写缓存策略。

调试信息查看
调试时候使用log4j的log4j.logger.org.hibernate.cache=debug,更方便看到ehcache的操作过程,主要用于调试过程,实际应用发布时候,请注释掉,以免影响性能。
在log4j.properties里加上这句log4j.logger.org.hibernate.cache=debug
Log信息如:
2007-03-01 17:58:23,718 http-8080-Processor23 DEBUG NonstrictReadWriteCache:71 - Caching: com.maf.family.entity.Family#62

Criteria查询
Session s=HibernateSessionFactory.getSession();
Criteria c=s.createCriteria(Resources.class);
c.setCacheable(true); //添加

SessionFactory也提供了移除缓存的方法
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.

但这样做很难维护

==========***********************=======================

 

我一般在程序处理大量数据的时候:

采用的是:

this.getHibernateSession().evict(object);

this.getHibernateSession().flush();
this.getHibernateSession().clear();

 

这三个方法,看各自所需。

 

分享到:
评论
2 楼 xiaofengtoo 2008-08-03  
本文就是一次请求大数据量的时候,但一次请求并没有结束,你不怎么处理,兄台给个好方案?
1 楼 supttkl 2008-07-05  
讲了满屏幕,都没到正点上.一般一次请求都会关闭Session.
this.getHibernateSession().evict(object);

this.getHibernateSession().flush();
this.getHibernateSession().clear();
你用这3个鸟玩意有什么用。

相关推荐

Global site tag (gtag.js) - Google Analytics