转载自http://blog.csdn.net/maoyeqiu/article/details/50209893

前两天总结了一下二级缓存和查询缓存的关系,但是又有一个新的问题,就是查询缓存缓存到二级缓存的数据,在第三次(第一次缓存中没有数据,查询数据库将对应的ID值存入到二级缓存中去,第二次如果是同一个Session那么将会把数据一级缓存中的数据返回,如果不是同一个Session而是同一个sessionfactory,那么将会把二级缓存中的数据返回,同时将数据放入到一级缓存中去)获取的时候,不使用查询缓存的方法,而是直接使用一级缓存的方法,能不能将缓存的数据获取到呢,我们现在验证一下。

首先开启一级缓存(默认)、二级缓存和查询缓存

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>
  9. <property name="hibernate.connection.password">root</property>
  10. <property name="hibernate.connection.username">root</property>
  11. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  12. <property name="show_sql">true</property>
  13. <property name="hbm2ddl.auto">create</property>
  14. <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
  15. <property name="hibernate.cache.use_query_cache">true</property>
  16. <!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 -->
  17. <property name="hibernate.cache.use_second_level_cache">true</property>
  18. <property name="hibernate.generate_statistics">true</property>
  19. <mapping class="hibernate.test.dto.DepartmentEntity"></mapping>
  20. </session-factory>
  21. </hibernate-configuration>

用到的缓存类

  1. package hibernate.test.dto;
  2. @Entity
  3. @Table(name = "DEPARTMENT", uniqueConstraints = {
  4. @UniqueConstraint(columnNames = "ID"),
  5. @UniqueConstraint(columnNames = "NAME") })
  6. @Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="department")
  7. public class DepartmentEntity implements Serializable {
  8. private static final long serialVersionUID = 1L;
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. @Column(name = "ID", unique = true, nullable = false)
  12. private Integer id;
  13. @Column(name = "NAME", unique = true, nullable = false, length = 100)
  14. private String name;
  15. public Integer getId() {
  16. return id;
  17. }
  18. public void setId(Integer id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. }

启动查询缓存将返回的实体存入到二级缓存中去

  1. //此方法向数据库中存入三条数据
  2. storeData();
  3. Session session = HibernateUtil.getSessionFactory().openSession();
  4. session.beginTransaction();
  5. //开启查询缓存, query.setCacheable(true);开启查询缓存
  6. Query query = session.createQuery("select s from DepartmentEntity s");
  7. query.setCacheable(true);
  8. List<DepartmentEntity> names = query.list();
  9. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {
  10. String name = it.next().getName();
  11. System.out.println(name);
  12. }
  13. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());

执行的结果:

hibernate: select department0_.ID as ID0_, department0_.NAME as NAME0_ from DEPARTMENT department0_
Human Resource
Humanne
Jhon
3

我们可以看到,产生了一条查询语句,将三个实体的名字输出,放入二级缓存的数量是3,都是没有问题的。这里比较容易犯的一个错误是查询的结果并不是实体,而是名字或者是其他属性,比如sql我们这么写:select s.name from DepartmentEntity s。存入二级缓存的数据是0,也就是没有存入到二级缓存中去。

再次调用上述方法:

  1. //开启查询缓存, query.setCacheable(true);开启查询缓存
  2. Query query = session.createQuery("select s from DepartmentEntity s");
  3. query.setCacheable(true);
  4. List<DepartmentEntity> names = query.list();
  5. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {
  6. String name = it.next().getName();
  7. System.out.println(name);
  8. }
  9. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  10. //同一个Session中再次查询,不会发出SQL语句到数据库
  11. query = session.createQuery("select s from DepartmentEntity s");
  12. query.setCacheable(true);
  13. names = query.list();
  14. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {
  15. String name = it.next().getName();
  16. System.out.println(name);
  17. }
  18. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  19. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());

执行的结果:

Hibernate: select department0_.ID as ID0_, department0_.NAME as NAME0_ from DEPARTMENT department0_
Human Resource
Humanne
Jhon
3
Human Resource
Humanne
Jhon
3
0

我们可以看到只产生了一条数据库查询语句,第二次直接从缓存中获取(注意,第二次调用的时候依然要写quey.setCacheable(true); ,我们模拟的是同一个方法的多次调用,不然的会产生数据库查询),那么问题来了,这个缓存指的是一级缓存还是二级缓存呢,我们看执行的结果二级缓存的命中是0,也就是说这里并没有用到二级缓存而是直接使用了一级缓存,前提是在同一个Session的情况下,在同一个session下执行的结果都会首先缓存到一级缓存中去,那么我们开一个新的Session会有什么样的不同结果呢

  1. //开启查询缓存, query.setCacheable(true);开启查询缓存
  2. Query query = session.createQuery("select s from DepartmentEntity s");
  3. query.setCacheable(true);
  4. List<DepartmentEntity> names = query.list();
  5. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {
  6. String name = it.next().getName();
  7. System.out.println(name);
  8. }
  9. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  10. //同一个Session中再次查询,不会发出SQL语句到数据库
  11. query = session.createQuery("select s from DepartmentEntity s");
  12. quey.setCacheable(true);
  13. names = query.list();
  14. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {
  15. String name = it.next().getName();
  16. System.out.println(name);
  17. }
  18. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  19. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());
  20. Session anotherSession = HibernateUtil.getSessionFactory().openSession();
  21. anotherSession.beginTransaction();
  22. query = anotherSession.createQuery("select s from DepartmentEntity s");
  23. query.setCacheable(true);
  24. names = query.list();
  25. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {
  26. String name = it.next().getName();
  27. System.out.println(name);
  28. }
  29. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  30. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());

执行结果:

  1. Hibernate: select department0_.ID as ID0_, department0_.NAME as NAME0_ from DEPARTMENT department0_
  2. Human Resource
  3. Humanne
  4. Jhon
  5. 3
  6. Human Resource
  7. Humanne
  8. Jhon
  9. 3
  10. 0
  11. Human Resource
  12. Humanne
  13. Jhon
  14. 3
  15. 3

我们看到运行的结果,三次执行都只是放入二级缓存的实例3个,也就是说二级缓存中只有三个实例。由于二级缓存是sessionfactory级别的当开启查询缓存将数据放入的二级缓存的时候是不受开了几个session影响的,所以尽管我们上边开启了2个session但是依旧是在二级缓存中有3个实体。这里还有一个问题是查询缓存的key值是如何定义的呢,导致了开启了3次查询缓存而只存入3条数据,如果key值不同的话,那么肯定是会存入9条数据,关于这个问题大家可以参考,这里。由于二级缓存是sessionfactory级别的,因此会直接将二级缓存中的数据取出,并存入到一级缓存中去。

我们在sql中只是查询实体的名字,我们来看一下查询缓存是如何缓存的

  1. //开启查询缓存, query.setCacheable(true);开启查询缓存
  2. ,Query query = session.createQuery("select s.name from DepartmentEntity s");
  3. query.setCacheable(true);
  4. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  5. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());
  6. //同一个Session中再次查询,不会发出SQL语句到数据库
  7. query = session.createQuery("select s.name from DepartmentEntity s");
  8. query.setCacheable(true);
  9. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  10. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());
  11. Session anotherSession = HibernateUtil.getSessionFactory().openSession();
  12. anotherSession.beginTransaction();
  13. query = anotherSession.createQuery("select s.name from DepartmentEntity s");
  14. query.setCacheable(true);
  15. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());
  16. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());

执行结果:

Hibernate: select department0_.NAME as col_0_0_ from DEPARTMENT department0_
0
0
0
0
0
0

从结果上我们可以看到只有一个数据库查询,然后是六个0,前两个0是没有将数据存入到二级缓存中去,中间两个0并且没有sql数据库查询说明,数据从缓存中获取,而且是一级缓存中的数据,后两个0我们重新开启了一个session,同样没有数据库查询,也就说是调用了二级缓存中的数据,也就说明了查询缓存也是sessionfactory级别的。

我们现在来验证一下我们刚才提出的问题,直接用load获取数据

  1. DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));
  2. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());

执行结果:

0

从结果来看,没有产生数据库查询二级缓存的命中是0,也就是说数据是从一级缓存中获取的,这就验证了我们一开始提到的答案。

总结:

1、一级缓存是session级别的,二级缓存和查询缓存都是sessionfactory级别的,查询缓存和二级缓存是一起来使用的

2、任何sql执行都会存入到同一个session的一级缓存中去

3、同时开启查询缓存和二级缓存,可以在不同session间共享缓存的结果

4、二级缓存缓存的是实体,不是属性

5、查询缓存的结果如果只是属性,那么查询缓存中存储的是id和属性的值,如果是实体的集合,那么查询缓存存储的只是实体的id,对应的实体会存储到二级缓存中去。

6、不同session间返回数据的顺序是,二级缓存先将数据返回,然后将数据存入本session的一级缓存中去,以便下次调用时的使用

Hibernate一级缓存、二级缓存以及查询缓存的关系的更多相关文章

  1. 说说自己对hibernate一级、二级、查询、缓存的理解。

    说说自己对hibernate一级.二级.查询.缓存的理解. 2016-03-14 21:36 421人阅读 评论(0) 收藏 举报  分类: web开发(19)  版权声明:本文为博主原创文章,未经博 ...

  2. ThinkPHP缓存技术(S(),F(),查询缓存,静态缓存)

    直接查看原网址 https://blog.csdn.net/u010081689/article/details/47976271

  3. hibernate缓存机制详细分析(一级、二级、查询缓存,非常清晰明白)

    本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相信 ...

  4. Hibernate第十二篇【二级缓存介绍、缓存策略、查询缓存、集合缓存】

    Hibernate二级缓存介绍 前面我们已经讲解过了一级缓存,一级缓存也就是Session缓存,只在Session的范围内有效-作用时间就在Session的作用域中,范围比较小 Hibernate为我 ...

  5. Hibernate中 一 二级缓存及查询缓存(2)

    缓存:缓存是什么,解决什么问题?  位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为缓存Cache.缓存目的:让数据更接近于应用程序,协调速度不匹配,使访问速度更 ...

  6. Hibernate中 一 二级缓存及查询缓存(1)

    最近趁有空学习了一下Hibernate的缓存,其包括一级缓存,二级缓存和查询缓存(有些是参照网络资源的): 一.一级缓存     一级缓存的生命周期和session的生命周期一致,当前sessioin ...

  7. 【Hibernate】 二级缓存及查询缓存

    一.Hibernate的二级缓存 1.1 类缓存区特点 缓存的是对象的散装的数据. 图一 Hibernate的二级缓存的散装数据 1.2 集合缓存区的特点: 缓存的是对象的id.需要依赖类缓冲区的配置 ...

  8. hibernate--一级和二级缓存(使用Ehcache)以及查询缓存

    https://blog.csdn.net/u012411414/article/details/50483185 有一下几点需要理清才行: 一级缓存是session缓存 session关闭就小时 二 ...

  9. Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...

  10. (十二)mybatis 查询缓存

    目录 什么是查询缓存 图解查询缓存 一级缓存 二级缓存 禁用二级缓存 刷新缓存 二级缓存应用场景 二级缓存局限性 什么是查询缓存 mybatis 在查询数据的时候,会将数据存储起来,下次再次查询相同的 ...

随机推荐

  1. Linux常见的进程调度算法

    进程调度:在操作系统中调度是指一种资源分配. 调度算法是指: 根据系统的资源分配策略所规定的资源分配算法. 操作系统管理了系统的有限资源,当有多个进程(或多个进程发出的请求)要使用这些资源时,因为资源 ...

  2. STL之内存处理

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL中与内存配置相关的类是allocator类,头文件为:#include<alllocator>这是一个模板类,用于内存的分配.对象 ...

  3. django中templates阅读笔记

    一.基本知识 1.模版是独立于django的,可以独立运行. 模版变量是用两个大括号括起来的字符串,表示变量.例如{{ person_name }} 模版标签,是用一对大括号和一对百分号括起来的,例如 ...

  4. 使struct对象拥有可变大小的数组——(C++深度探索)

    首先摘录<Inside The C++ Object Model>中的一段话: 把单一元素的数组放在一个struct的尾端,于是每个 struct objects 可以拥有可变大小的数组: ...

  5. HDU 4968 Improving the GPA(dp)

    HDU 4968 Improving the GPA 题目链接 dp.最大最小分别dp一次,dp[i][j]表示第i个人,还有j分的情况,分数能够减掉60最为状态 代码: #include <c ...

  6. php四种排序算法实现代码

    分享php排序的四种算法与代码. 冒泡:function bubble_sort($arr){ $num = count($arr); for($i=0;$i<$num;$i++){ for($ ...

  7. angular学习笔记(七)-迭代2

    视图的迭代和它的ng-repeat属性绑定的数据是实时绑定的,一旦数据发生了改变,视图也会立即更新迭代. 还是刚才的那个例子,给它添加一个添加数据按钮和一个删除数据按钮. <!DOCTYPE h ...

  8. VS2012插件推荐

    其实Metro的感觉有了,但是这两种配色都不太喜欢..白色太扎眼,黑色太瞎眼……有木有灰色的啊喂(有没有其他主题?看后面↓) 黑色主题绝对是熬夜码农必备……但是十分不适合在白天使用. 下面来分享几个好 ...

  9. C/C++数组名与指针区别深入探索(转载)

    转载自: C/C++数组名与指针区别深入探索 引言 指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很多程序设计者就被搞糊涂了.而许多的大学老师, ...

  10. ELK的索引的坑——Kibana的图形化(Tile Map)

    如果想通过ELK展示地图, 需要将索引名称修改为:logstash*的格式 否则location字段不会修改成geo_point的形式. 详情参考:http://blog.csdn.net/yangg ...