读书人

hibernate 二级缓存配备

发布时间: 2012-09-07 10:38:15 作者: rapoo

hibernate 二级缓存配置
hibernate 二级缓存配置
第一步 新建ehcache.xml 放到 src 目录下,以下文件具体参数意义,我这不多说了,你从百度查一下“ehcache 配置” 会有很详细的介绍,

02 在这呢,我只说一下,基本配置,并如何知道已成功应用 二级缓存

03 <ehcache Check="false">

04 <diskStore path="java.io.tmpdir" />

05 <cacheManagerEventListenerFactory properties="" />

06 <defaultCache

07 maxElementsInMemory="1000"

08 eternal="true"

09 overflowToDisk="true"

10 timeToIdleSeconds="120"

11 timeToLiveSeconds="180"

12 diskPersistent="false"

13 diskExpiryThreadIntervalSeconds="60"/>

14 </ehcache>

15 第二步 hibernate 参数配置。 以下为 spring applicationContext 内配置方式
查看源码打印?1 <prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>

2 <prop key="hibernate.cache.use_second_level_cache">true</prop>

3 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>

4 <prop key="hibernate.generate_statistics">true</prop>
第三步 在被应用缓存的XXX.hbm.xml 文件中加入下
查看源码打印?1 <cache usage="read-write" /> 详细参数 自己查询

第四步 打开日志
log4j.logger.net.sf.ehcache=debug

注意:
1. 如果使用 getHibernateTemplate 查询要设置以下属性

查看源码打印?1 getHibernateTemplate().setCacheQueries(true);
2. 加入以下代码查看 是否 配置成功
查看源码打印?01 getHibernateTemplate().execute(new HibernateCallback()

02 {

03

04 public Object doInHibernate(Session arg0) throws HibernateException,

05 SQLException

06 {

07 Statistics message=arg0.getSessionFactory().getStatistics();

08 System.out.println("二级缓存命中数:"+message.getSecondLevelCacheHitCount());

09 return null;

10 }

11 });
如第二次查询 二级缓存命中数大于0 配置成功。

读书人网 >编程

热点推荐