hibernate 二級緩存配置

306020 12年前發布 | 72K 次閱讀 Hibernate 持久層框架

 第一步 新建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 內配置方式
    <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>
第三步 在被應用緩存的XXX.hbm.xml 文件中加入下
<cache usage="read-write" /> 詳細參數  自己查詢

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

注意:
1. 如果使用 getHibernateTemplate 查詢要設置以下屬性

getHibernateTemplate().setCacheQueries(true);
2. 加入以下代碼查看 是否 配置成功
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 配置成功。  
 本文由用戶 306020 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!