zoukankan      html  css  js  c++  java
  • hibernate 二级缓存配置

    第一步 新建ehcache.xml  放到 src 目录下,以下文件具体参数意义,我这不多说了,你从百度查一下“ehcache 配置” 会有很详细的介绍,
    02 在这呢,我只说一下,基本配置,并如何知道已成功应用 二级缓存
    03 <ehcache Check="false">
    04     <diskStore path="java.io.tmpdir" />
    05     <cacheManagerEventListenerFactory class="" 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 配置成功

  • 相关阅读:
    ubuntu
    ubuntu
    ubuntu14.04,安装Gnome 15.10 (桌面)
    ubuntu14.04,安装Gnome 15.10 (桌面)
    Ubuntu 14.04.3 LTS如何安装谷歌输入法
    Ubuntu 14.04.3 LTS如何安装谷歌输入法
    ubuntu 安装 删除 卸载 Deb 包文件
    失去爆破音规律
    单词发音规律
    英式音标和美式音标的差异
  • 原文地址:https://www.cnblogs.com/zhouwenwu/p/2336469.html
Copyright © 2011-2022 走看看