二级缓存:

  定义:

      1.二级缓存被称为进程级缓存或者sessionFactory级缓存,二级缓存可以被所有session共享

      2.二级缓存的生命周期和sessionFactory生命周期一样(sessionFactory生命周期需要在配置中设定),sessionFactory可以管理二级缓存

      3.二级缓存的数据是一级缓存放入的,也就是说二级缓存与一级缓存雷同,也是存放的实体类对象,对普通属性不执行缓存策略,不同的是生命周期的不同,二级缓存可以共享

  二级缓存需要配置才能够生效:

    1.引入第三方包ehcache

    2.在hibernate.cfg配置:

    我们需要配置三个地方: 一个是驱动提供,二个是 开启二级缓存,三个是配置需要缓存的实体类

 <!-- 二级缓存驱动提供 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
      
<!-- 缓存配置 -->
<class-cache usage="read-only" class="entity.Grade"/>
<!--与grade关联的对象 -->
<class-cache usage="read-only" class="entity.Student"/>
<collection-cache usage="read-only" collection="entity.Grade.stu"/>

    注意:

      1.由于hibernate.cache.use_second_level_cache默认是开启的,我们也可以省略不写

      2.常用缓存策略:read-only / read-write

      3.缓存原则:读远大于写的时候使用缓存

    二级缓存在配置中开启时:

      当业务不需要使用二级缓存时:

      HibernateUtils.getSessionFactory.evict(实体对象.class);

      or

      HibernateUtils.getSessionFactory.evict(实体对象.class,1);//指定具体

      当我们不希望查询结果保存到二级缓存中:

      session.setCacheMode(CacheMode.IGNRE);//可以取消一级缓存和二级缓存之间的交互,这样可以达到当前sesison不能狗向二级缓存中保存数据

    补充:

      昨天笔记中提到大数据缓存处理,当我们开启二级缓存时,session.flush()    session.clear()只能清除session级缓存,那么sessionFactory级/二级缓存怎么处理:

      这里我在视频中学习到一种方法:

      在save 或这查询前  取消一级缓存和二级缓存之间的交互,达到目的  

    

      

Hibernate <二级缓存>的更多相关文章

  1. Hibernate ——二级缓存

    一.Hibernate 二级缓存 1.Hibernate 二级缓存是 SessionFactory 级别的缓存. 2.二级缓存分为两类: (1)Hibernate内置二级缓存 (2)外置缓存,可配置的 ...

  2. 配置Hibernate二级缓存时,不能初始化RegionFactory的解决办法

    配置Hibernate 二级缓存时,出现以下bug提示: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder&quo ...

  3. 配置Hibernate二级缓存步骤

    配置Hibernate二级缓存步骤: 加入二级缓存的jar包及配置文件 jar包位置:hibernate-release-4.1.8.Final\lib\optional\ehcache下所有jar包 ...

  4. Hibernate 二级缓存 总结整理(转)

    和<Hibernate 关系映射 收集.总结整理> 一样,本篇文章也是我很早之前收集.总结整理的,在此也发上来 希望对大家有用.因为是很早之前写的,不当之处请指正. 1.缓存:缓存是什么, ...

  5. Hibernate(十六):Hibernate二级缓存

    Hibernate缓存 缓存(Cache):计算机领域非常通用的概念.它介于应用程序和永久性数据存储源(如磁盘上的文件或者数据库)之间,起作用是降低应用程序直接读取永久性数据存储源的频率,从而提高应用 ...

  6. hibernate二级缓存demo2

    @Test public void hello3(){ Session session=sessionFactory.openSession(); List list = session.create ...

  7. hibernate二级缓存整合

    <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...

  8. ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存

    ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存 hibernate  : Hibernate是一个持久层框架,经常访问物理数据库 ...

  9. spring boot集成ehcache 2.x 用于hibernate二级缓存

    https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...

  10. js相关(easyUI),触发器,ant,jbpm,hibernate二级缓存ehcache,Javamail,Lucene,jqplot,WebService,regex,struts2,oracle表空间

    *********************************************js相关********************************************* // 在指 ...

随机推荐

  1. ssh secure shell

    ssh secure shell 和securecrt xhell一样,都是终端工具

  2. DDoS deflate+iptables防御轻量级ddos攻击

    一.查看攻击者ip #netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n 二.安装ddos deflate ...

  3. SevenZip.pas BUG修改版 - 20160613

    原始版本: Henri Gourvest <hgourvest@gmail.com> 1.2版本 BUG修改: 1.对于文件名中带有空格的文件, 无法压缩, 原因是1488行, 压缩调用的 ...

  4. Java 操作符

    子系统的特点 instanceof 操作符 Animal类是Dog的直接父类,Creature类和Object类是Dog的间接父类,因此 Dog dog=new Dog(); System.out.p ...

  5. 使用百度编辑器时,报错:从客户端("...)中检测到有潜在危险的 Request.Form 值

    ajax请求失败,提交不上去 起初没找到报错的这句话,可能由于我用了其他第三方插件的原因,后来改动了一下,出现了这个错误 然后百度得到答案:http://www.cnblogs.com/tearer/ ...

  6. ubuntu编译运行xv6

    最近想找个简单的类Unix系统学习下, xv6不错的, 所有代码加起来不到一万行,首先把代码跑起来还是很重要的. # 下载xv6源码并编译 git clone git://pdos.csail.mit ...

  7. react-native 计算时间差

    //计算时间var sendTime = this.props.obj.send_time; console.log(sendTime); //当前时间var nowTime = (new Date( ...

  8. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. 将Cocos2dX渲染到MFC窗口上

    引用:http://www.cnblogs.com/windeer/archive/2012/11/18/2767750.html 引言 现在智能手机已经慢慢进入大众化,移动类应用开始火爆起来,游戏类 ...

  10. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数006, image,影像处理(像素图)

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数006, image,影像处理(像素图) 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“* ...