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 配置成功

  • 相关阅读:
    解决Maven下载速度缓慢问题
    IntelliJ IDEA 最新激活码
    Googel 浏览器 模拟发送请求工具--Advanced REST Client
    Firefox火狐 浏览器接口调试工具 JSON 格式化
    修复/lib/ld-linux.so.2: bad ELF interpreter: No such file or directory问题
    解决Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid
    configure: error: You need a C++ compiler for C++ support.[系统缺少c++环境]
    解决编译apache出现的问题:configure: error: APR not found . Please read the documentation
    centos6 Linux安装redis 2.6.14
    Nginx+Tomcat负载均衡配置
  • 原文地址:https://www.cnblogs.com/zhouwenwu/p/2336469.html
Copyright © 2011-2022 走看看