hibernate 二级缓存配置

306020 12年前
     <pre class="brush:xml; toolbar: true; auto-links: false;"> 第一步 新建ehcache.xml  放到 src 目录下,以下文件具体参数意义,我这不多说了,你从百度查一下“ehcache 配置” 会有很详细的介绍, 在这呢,我只说一下,基本配置,并如何知道已成功应用 二级缓存 <ehcache Check="false">     <diskStore path="java.io.tmpdir" />  <cacheManagerEventListenerFactory class="" properties="" />  <defaultCache   maxElementsInMemory="1000"             eternal="true"             overflowToDisk="true"            timeToIdleSeconds="120"            timeToLiveSeconds="180"            diskPersistent="false"            diskExpiryThreadIntervalSeconds="60"/>    </ehcache> 第二步 hibernate 参数配置。 以下为 spring applicationContext 内配置方式</pre>    <pre class="brush:xml; toolbar: true; auto-links: false;"> <prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>  <prop key="hibernate.cache.use_second_level_cache">true</prop>  <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>  <prop key="hibernate.generate_statistics">true</prop></pre>第三步 在被应用缓存的XXX.hbm.xml 文件中加入下    <br />    <pre class="brush:xml; toolbar: true; auto-links: false;"><cache usage="read-write" /> 详细参数  自己查询</pre>    <br /> 第四步 打开日志    <br /> log4j.logger.net.sf.ehcache=debug    <br />    <br /> 注意:    <br /> 1. 如果使用 getHibernateTemplate 查询要设置以下属性    <br />    <br />    <pre class="brush:java; toolbar: true; auto-links: false;">getHibernateTemplate().setCacheQueries(true);</pre>2. 加入以下代码查看 是否 配置成功    <br />    <pre class="brush:java; toolbar: true; auto-links: false;">getHibernateTemplate().execute(new HibernateCallback()   {        public Object doInHibernate(Session arg0) throws HibernateException,      SQLException    {     Statistics message=arg0.getSessionFactory().getStatistics();     System.out.println("二级缓存命中数:"+message.getSecondLevelCacheHitCount());     return null;    }   });</pre>如第二次查询 二级缓存命中数大于0 配置成功。