hibernate--一级和二级缓存(使用Ehcache)以及查询缓存
https://blog.csdn.net/u012411414/article/details/50483185
有一下几点需要理清才行:
一级缓存是session缓存 session关闭就小时
二级缓存是sessionFactory级别的缓存 一个应用程序只有一个 多个线程共享 不要把经常修改的对象放到二级缓存中 二级缓存中放一些查询的对象
1 首先是在hibernate,cfg.xml文件中进行配置:
添加下列配置项
- <property name="hibernate.cache.use_second_level_cache">true</property>
- <!-- 使用哪种缓存提供的类 哪种缓存 -->
- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
- <!-- 使用查询缓存-->
- <property name="hibernate.cache.use_query_cache">true</property>
- <!-- ehcache.xml的配置文件路径 -->
- <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
在需要使用二级缓存的对象配置文件上:
- <class name="Student" table="t_stu">
- <!-- <cache usage="read-only" /> -->
- <id name="id">
- <generator class="native"></generator>
- </id>
- <!-- 注意:version 一定要加在ID后面 property前面 -->
- <version name="version" />
- <property name="name" />
- <property name="sex" />
- <many-to-one name="classroom" column="cid" fetch="join" />
- </class>
ehcache.xml中的配置:
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- />
- <!-- 每一个独立的cache可以单独为不同的对象进行设置 如果找不到就用默认的cache-->
- <cache name="com.itany.model.Student"
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- overflowToDisk="true"
- />
2 N+1问题
如果使用iterator返回列表 对于hibernate而言
它仅仅是查询了id列表的SQL 在进行iterator迭代的时候
再会根据id一个个去数据库查询具体对象 因此发出多条SQL 这就是典型的N+1问题 避免它
就是不使用iterator iterator存在的原因
使用的地方:
因为有可能需要查询2次 第一次list全部查询出来 存在二级缓存中 第二次 用Iterator数据库查id,再根据ID从二级缓存的对象中查询更快
- session = HibernateUtil.openSession();
- Iterator<Student> it = session.createQuery("from Student").setFirstResult(0).setMaxResults(12).iterate();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
3 重要:二级缓存缓存的是对象 查询缓存缓存的是ID 这是两种不同的缓存 谁也不依赖谁 查询缓存也是sessionFactory级别的缓存
查询缓存是针对HQL语句的缓存 查询缓只缓存ID 而不会缓存对象
1 但是使用查询缓存最好打开二级缓存 因为在查询缓存中查到ID后可以直接去
二级缓存中查找 不然的话又是N+1问题 多次根据查询缓存中的ID去数据库查询
2 若打开了二级缓存 但是没有打开查询缓存(HQL不一致、参数不一致、查询缓存没开、
setCacheable(true)没写等原因)那么还是会直接从数据库中查询一次、
因为需要借助查询缓存查ID再到二级缓存中查对象
3 注意:load方式可以直接从二级缓存中查对象 不必借助查询缓存
4如果取得只是某些属性(而不是完整对象) 那么不会进行二级缓存见test04
5 查询缓存 工作顺序:如果正常开启了查询缓存(HQL完全一致 且set(True)),先去查询缓存中查ID
再根据查询到的ID去二级缓存中查对象 若此时二级缓存打开了 那么不会发SQL
若二级缓存没打开 那么此时会再次根据查到的ID去数据库中查询
4 代码中使用查询缓存
- List<Object[]> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张%").list();//开启查询缓存 查询缓存也是sessionFactory级别的缓存
5 此时关闭了二级缓存(存对象)
打开了查询缓存(存ID) 第二个人查询缓存只查到ID 会根据ID 去二级缓存查对象 但是没有 所以去数据库查 发出大量SQL
6 查询缓存很少使用 因为很少HQL完全一样
因为只要HQL语句不一样 就不会开启查询缓存
只有两个完全一样的 并且参数都一样的 才能使用查询缓存
- public class TestSecondCache
- {
- /*
- * 重要:二级缓存缓存的是对象 查询缓存缓存的是ID 这是两种不同的缓存
- * 谁也不依赖谁
- * 1 但是使用查询缓存最好打开二级缓存 因为在查询缓存中查到ID后可以直接去
- * 二级缓存中查找 不然的话又是N+1问题 多次根据查询缓存中的ID去数据库查询
- * 2 若打开了二级缓存 但是没有打开查询缓存(HQL不一致、参数不一致、查询缓存没开、
- * setCacheable(true)没写等原因)那么还是会直接从数据库中查询一次、
- * 因为需要借助查询缓存查ID再到二级缓存中查对象
- * 3 注意:load方式可以直接从二级缓存中查对象 不必借助查询缓存
- *
- * 如果取得只是某些属性(而不是完整对象) 那么不会进行二级缓存见test04
- */
- @Test
- public void test01()
- {
- /*
- * 不可以缓存 Ehcache是看SQL是否一样
- */
- Session session = null;
- try
- {
- session = HibernateUtil.openSession();
- Student s=(Student)session.load(Student.class,1);
- System.out.println(s.getName());
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- Session session2 = null;
- /*
- * 此时session已经关闭 但再次查询 仍然可以不发SQL 二级缓存起作用了
- */
- try
- {
- session2 = HibernateUtil.openSession();
- Student s2=(Student)session2.load(Student.class,1);//这种写法 根据ID去二级缓存中查询
- System.out.println(s2.getName());
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session2)
- HibernateUtil.closeSession(session2);
- }
- }
- @Test
- public void test02()
- {
- /*
- *
- */
- Session session = null;
- try
- {
- session = HibernateUtil.openSession();
- Student s=(Student)session.load(Student.class,1);
- System.out.println(s.getName());
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- Session session2 = null;
- Transaction trans=null;
- /*
- * 会报错 因为二级缓存 在Student上设置的是 read-only 因此不能写入
- * 最好不要设置 read write因为hibernate要频繁的加锁 解锁 影响效率 还不如不用二级缓存
- */
- try
- {
- session2 = HibernateUtil.openSession();
- trans=session2.beginTransaction();
- Student s2=(Student)session2.load(Student.class,1);
- s2.setName("zhangasnsss");
- System.out.println(s2.getName());
- trans.commit();
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session2)
- HibernateUtil.closeSession(session2);
- }
- }
- @Test
- public void test03()
- {
- /*
- *
- */
- Session session = null;
- Transaction trans = null;
- try
- {
- session = HibernateUtil.openSession();
- List<Student> ls = session.createQuery("from Student").list();
- Iterator<Student> it = ls.iterator();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- try
- {
- session = HibernateUtil.openSession();
- Student s=(Student)session.load(Student.class,1);
- System.out.println(s.getName());
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test04()
- {
- /*
- *
- */
- Session session = null;
- Transaction trans = null;
- try
- {
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu.id,stu.name from Student stu").list();
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /*
- * 以上代码仅仅去了id和name 而二级缓存是缓存对象的 所以上一段代码不会使用二级缓存
- * 此时就会发出相应的SQL
- */
- try
- {
- session = HibernateUtil.openSession();
- Student s=(Student)session.load(Student.class,1);
- System.out.println(s.getName());
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test05()
- {
- /*
- * iterator 作用就是配合二级缓存使用 最好
- */
- Session session = null;
- Transaction trans = null;
- try
- { //一条SQL
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu from Student stu").list();
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /* 1条SQL
- * 由于学生的对象经过上面查询对象之后 已经存在于二级缓存之中
- * 此时正好直接使用iterator 首先会查询通过1条取ID的语句 然后再获取对象时候去二级缓存中查询
- * 有的话 直接用 不会产生N+1问题
- *
- * 若没二级缓存 我们直接使用iterator会首先产生一条查ID的语句
- * 再在获取对象的时候再去数据库中取 那么这是就会产生N+1问题
- */
- try
- {
- session = HibernateUtil.openSession();
- Iterator<Student> it = session.createQuery("from Student").iterate();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test06()
- {
- /*
- * 与上面一个几乎一样 区别在于iterator换成list()
- * 虽然第一次已经二级缓存了 但是不能决定我再去不去数据库中查询
- *
- * 重要 :如果希望第二次不发SQL 那就要使用查询缓存
- * 查询缓存是针对HQL语句的缓存 查询缓只缓存ID 而不会缓存对象
- */
- Session session = null;
- Transaction trans = null;
- try
- { //一条SQL
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu from Student stu").list();
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /* 1条SQL 和上一条一模一样
- *
- */
- try
- {
- session = HibernateUtil.openSession();
- List<Student> ls = session.createQuery("select stu from Student stu").list();
- Iterator<Student> it=ls.iterator();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test07()
- {
- /*
- * 设置了查询缓存为true
- */
- Session session = null;
- Transaction trans = null;
- try
- { //一条SQL
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu from Student stu")
- .setCacheable(true).list();//开启查询缓存 查询缓存也是sessionFactory级别的缓存
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /*0条SQL
- *
- */
- try
- {
- session = HibernateUtil.openSession();
- List<Student> ls = session.createQuery("select stu from Student stu")
- .setCacheable(true).list();
- Iterator<Student> it=ls.iterator();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test08()
- {
- /*
- * 此时无法使用查询缓存 查询缓存很少使用 因为很少HQL完全一样
- * 因为只要HQL语句不一样 就不会开启查询缓存
- * 只有两个完全一样的 并且参数都一样的 才能使用查询缓存
- */
- Session session = null;
- Transaction trans = null;
- try
- { //一条SQL
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张%").list();//开启查询缓存 查询缓存也是sessionFactory级别的缓存
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /*
- *
- */
- try
- {
- //一条SQL
- session = HibernateUtil.openSession();
- List<Student> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%王%").list();
- Iterator<Student> it=ls.iterator();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test09()
- {
- /*
- * 此时关闭了二级缓存(存对象)
- * 打开了查询缓存(存ID)
- * !!!工作顺序:如果正常开启了查询缓存(HQL完全一致 且set(True)),先去查询缓存中查ID
- * 再根据查询到的ID去二级缓存中查对象 若此时二级缓存打开了 那么不会发SQL
- * 若二级缓存没打开 那么此时会再次根据查到的ID去数据库中查询
- */
- Session session = null;
- Transaction trans = null;
- try
- { //一条SQL
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张%").list();//开启查询缓存 查询缓存也是sessionFactory级别的缓存
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /*
- *
- */
- try
- {
- //此时关闭了student的二级缓存 打开了查询缓存 发出大量SQL
- session = HibernateUtil.openSession();
- List<Student> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张%").list();
- Iterator<Student> it=ls.iterator();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test10()
- {
- /*
- * 此时打开了二级缓存(存对象)
- * 打开了查询缓存(存ID)
- * !!!!工作顺序:如果正常开启了查询缓存,先去查询缓存中查ID
- * 再根据查询到的ID去二级缓存中查对象 若此时二级缓存打开了 那么不会发SQL
- * 若二级缓存没打开 那么此时会再次根据查到的ID去数据库中查询
- */
- Session session = null;
- Transaction trans = null;
- try
- { //一条SQL
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张%").list();//开启查询缓存 查询缓存也是sessionFactory级别的缓存
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /*
- *
- */
- try
- {
- //此时打开了student的二级缓存 打开了查询缓存 不发SQL
- session = HibernateUtil.openSession();
- List<Student> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张%").list();
- Iterator<Student> it=ls.iterator();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- @Test
- public void test11()
- {
- /*
- * 此时打开了二级缓存(存对象)
- * 打开了查询缓存(存ID)
- * !!!!工作顺序:如果正常开启了查询缓存,先去查询缓存中查ID
- * 再根据查询到的ID去二级缓存中查对象 若此时二级缓存打开了 那么不会发SQL
- * 若二级缓存没打开 那么此时会再次根据查到的ID去数据库中查询
- */
- Session session = null;
- Transaction trans = null;
- try
- { //一条SQL
- session = HibernateUtil.openSession();
- List<Object[]> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张%").list();//开启查询缓存 查询缓存也是sessionFactory级别的缓存
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- /*
- *
- */
- try
- {
- //此时虽然打开了student的二级缓存 但是不能使用查询缓存(HQL不一致)导致没法通过查询缓存查ID去
- //二级缓存中查对象
- //所以仍然发出一条SQL 从数据库中查询
- session = HibernateUtil.openSession();
- List<Student> ls = session.createQuery("select stu from Student stu where name like ?")
- .setCacheable(true).setParameter(0, "%张三%").list();
- Iterator<Student> it=ls.iterator();
- while (it.hasNext())
- {
- Student stu = it.next();
- System.out.println(stu.getName());
- }
- }
- catch (HibernateException e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (null != session)
- HibernateUtil.closeSession(session);
- }
- }
- }
hibernate--一级和二级缓存(使用Ehcache)以及查询缓存的更多相关文章
- 说说自己对hibernate一级、二级、查询、缓存的理解。
说说自己对hibernate一级.二级.查询.缓存的理解. 2016-03-14 21:36 421人阅读 评论(0) 收藏 举报 分类: web开发(19) 版权声明:本文为博主原创文章,未经博 ...
- mybatis中二级缓存整合ehcache实现分布式缓存
mybatis自带二级缓存,但是这个缓存是单服务器工作,无法实现分布式缓存.那么什么是分布式缓存呢?假设现在有两个服务器1和2,用户访问的时候访问了1服务器,查询后的缓存就会放在1服务器上,假设现在有 ...
- ThinkPHP缓存技术(S(),F(),查询缓存,静态缓存)
直接查看原网址 https://blog.csdn.net/u010081689/article/details/47976271
- hibernate缓存机制详细分析(一级、二级、查询缓存,非常清晰明白)
本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相信 ...
- [原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓存,时间戳缓存
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring控制Hibernate的缓存机制ehcache
首先在spring.xml中进入bean <prop key="hibernate.cache.use_second_level_cache">true</pro ...
- Hibernate中 一 二级缓存及查询缓存(1)
最近趁有空学习了一下Hibernate的缓存,其包括一级缓存,二级缓存和查询缓存(有些是参照网络资源的): 一.一级缓存 一级缓存的生命周期和session的生命周期一致,当前sessioin ...
- 【Hibernate】 二级缓存及查询缓存
一.Hibernate的二级缓存 1.1 类缓存区特点 缓存的是对象的散装的数据. 图一 Hibernate的二级缓存的散装数据 1.2 集合缓存区的特点: 缓存的是对象的id.需要依赖类缓冲区的配置 ...
- Java三大框架之——Hibernate中的三种数据持久状态和缓存机制
Hibernate中的三种状态 瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...
随机推荐
- 20145312《网络对抗》MSF基础
20145312<网络对抗>MSF基础 实验要求 1.掌握metasploit的基本应用方式 2.掌握常用的三种攻击方式的思路 实验问答 用自己的话解释什么是exploit.payload ...
- 关于activity生命周期,启动模式和tag
Acticity启动模式 1.standard:Activity的默认加载方法,该方法会通过跳转到一个新的activity,同时将该实例压入到栈中(不管该activity是否已经存在在Task栈中,都 ...
- 【转载】浅谈JavaScript,let和var定义变量的区别
了解JS与ES5与ES6区别 JS语言 JavaScript一种动态类型.弱类型.基于原型的客户端脚本语言,用来给HTML网页增加动态功能. 动态: 在运行时确定数据类型.变量使用之前不需要类型声明, ...
- 如果恨一个程序员,忽悠他去做iOS开发
如果你恨一个程序员,忽悠他去做iOS开发.不管他背景是cobel还是 java,送他一本iOS开发的书.这种书最好是国人写的,容易以偏概全一点,相比洋鬼子的书,更容易学到皮毛.这叫舍不得孩子套不着狼, ...
- P1471 方差
题目 luogu 思路 \[\frac{1}{n}*\sum_{1}^{n}( a_{i}-A)^{2}\] \[\frac{1}{n}*\sum_{1}^{n}( a_{i}^2-2*A*a_{i} ...
- 打印图形|2014年蓝桥杯B组题解析第五题-fishers
打印图形 小明在X星球的城堡中发现了如下图形和文字: rank=3 rank=5 rank = 6 小明开动脑筋,编写了如下的程序,实现该图形的打印. 答案:f(a, rank-1, row, col ...
- SPOJ Hacking(字典树 + 搜索)题解
思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...
- C#中的异步编程模式
异步编程模型(APM) 基于事件的异步编程模式 基于任务的异步模式 Async Await编程 关于C#,可以看看Learning Hard的博客
- C#学习笔记(三):逻辑关系运算符和if语句
条件语句 分支语句和循环语句是程序里最重要的逻辑. IF语句.分支语句.循环语句 using System; using System.Collections.Generic; using Syste ...
- ElasticSearch 5.4 安装
1. 前期准备 环境准备 IP地址 操作系统 内存 192.168.1.10 centos 7 16 192.168.1.11 centos 7 16 192.168.1.12 centos ...