目录

1.概念

Hibernate提供的缓存

有一级缓存、二级缓存。 目的是为了减少对数据库的访问次数,提升程序执行效率!

1.1一级缓存:

     基于Session的缓存,缓存内容只在当前session有效,session关闭,缓存内容失效!
特点:
作用范围较小! 缓存的事件短。
缓存效果不明显。

1.2二级缓存:

     Hibernate提供了基于应用程序级别的缓存, 可以跨多个session,即不同的session都可以访问缓存数据。 这个换存也叫二级缓存。
Hibernate提供的二级缓存有默认的实现,且是一种可插配的缓存框架!如果用户想用二级缓存,只需要在hibernate.cfg.xml中配置即可;不想用,直接移除,不影响代码。
如果用户觉得hibernate提供的框架框架不好用,自己可以换其他的缓存框架或自己实现缓存框架都可以。

2.使用方法

查看hibernate.properties配置文件,二级缓存如何配置?


---Second-level Cache---


-hibernate.cache.use_second_level_cache false【二级缓存默认不开启,需要手动开启】

-hibernate.cache.use_query_cache true 【开启查询缓存】

--choose a cache implementation 【二级缓存框架的实现】

-hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider

-hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProvider

-hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider 默认实现

-hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider

-hibernate.cache.provider_class org.hibernate.cache.OSCacheProvider

-hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider

二级缓存,使用步骤

  1. 开启二级缓存

    2)指定缓存框架

    3)指定那些类加入二级缓存

    4)测试

    测试二级缓存!

3.策略

3.1缓存策略

<class-cache usage="read-only"/>     放入二级缓存的对象,只读;
<class-cache usage="nonstrict-read-write"/> 非严格的读写
<class-cache usage="read-write"/> 读写; 放入二级缓存的对象可以读、写;
<class-cache usage="transactional"/> (基于事务的策略)

3.2集合缓存

<!-- 集合缓存[集合缓存的元素对象,也加加入二级缓存] -->
<collection-cache
usage="read-write" collection="cn.itcast.b_second_cache.Dept.emps"/>

3.3查询缓存



``

 # 4.实例
```xml
<!--****************** 【二级缓存配置】****************** -->
<!-- a. 开启二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- b. 指定使用哪一个缓存框架(默认提供的) -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!-- 开启查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
<!-- c. 指定哪一些类,需要加入二级缓存 -->
<class-cache usage="read-write"class="cn.itcast.b_second_cache.Dept"/>
<class-cache usage="read-only"class="cn.itcast.b_second_cache.Employee"/>
<!-- 集合缓存[集合缓存的元素对象,也加加入二级缓存] -->
<collection-cache usage="read-write"collection="cn.itcast.b_second_cache.Dept.emps"/>
public class App {

    private static SessionFactory sf;
static {
sf = new Configuration()
.configure()
.addClass(Dept.class)
.addClass(Employee.class) // 测试时候使用
.buildSessionFactory();
}
// 1. 测试二级缓存的使用
// 没有/有用 二级缓存
@Test
public void testCache() {
Session session1 = sf.openSession();
session1.beginTransaction();
// a. 查询一次
Dept dept = (Dept) session1.get(Dept.class, 10);
dept.getEmps().size();// 集合
session1.getTransaction().commit();
session1.close(); System.out.println("------"); // 第二个session
Session session2 = sf.openSession();
session2.beginTransaction();
// a. 查询一次
dept = (Dept) session2.get(Dept.class, 10);// 二级缓存配置好; 这里不查询数据库
dept.getEmps().size(); session2.getTransaction().commit();
session2.close();
} @Test
public void listCache() {
Session session1 = sf.openSession();
session1.beginTransaction();
// HQL查询 【setCacheable 指定从二级缓存找,或者是放入二级缓存】
Query q = session1.createQuery("from Dept").setCacheable(true);
System.out.println(q.list());
session1.getTransaction().commit();
session1.close(); Session session2 = sf.openSession();
session2.beginTransaction();
q = session2.createQuery("from Dept").setCacheable(true);
System.out.println(q.list());// 不查询数据库: 需要开启查询缓存
session2.getTransaction().commit();
session2.close();
}
}

5. Seesion管理

    public void testSession() throws Exception {
//openSession: 创建Session, 每次都会创建一个新的session
Session session1 = sf.openSession();
Session session2 = sf.openSession();
System.out.println(session1 == session2);
session1.close();
session2.close(); //getCurrentSession 创建或者获取session
// 线程的方式创建session
// 一定要配置:<property name="hibernate.current_session_context_class">thread</property>
Session session3 = sf.getCurrentSession();// 创建session,绑定到线程
Session session4 = sf.getCurrentSession();// 从当前访问线程获取session
System.out.println(session3 == session4); // 关闭 【以线程方式创建的session,可以不用关闭; 线程结束session自动关闭】
//session3.close();
//session4.close(); 报错,因为同一个session已经关闭了!

29.Hibernate-二级缓存和session管理.md的更多相关文章

  1. hibernate学习笔记第七天:二级缓存和session管理

    二级缓存配置 1.导入ehcache对应的三个jar包 ehcache/*.jar 2.配置hibernate使用二级缓存 2.1设置当前环境开始二级缓存的使用 <property name=& ...

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

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

  3. Hibernate ——二级缓存

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

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

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

  5. Hibernate二级缓存原理

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

  6. 笔记:Hibernate 二级缓存

    Hibernate 包括二个级别的缓存,默认的总是启用Session级别的一级缓存,可选的 SessionFactory 级别的二级缓存,Session级别的一级缓存,但应用保存持久化实体.修改持久化 ...

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

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

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

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

  9. Hibernate 二级缓存配置

    详见:https://www.cnblogs.com/Junsept/p/7324981.html Hibernate的cache管理: Cache就是缓存,它往往是提高系统性能的最重要手段,对数据起 ...

随机推荐

  1. 剑指Offer 47. 求1+2+3+...+n (其他)

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 题目地址 https://www.nowcod ...

  2. L2-018. 多项式A除以B*

    L2-018. 多项式A除以B 参考博客 #include <iostream> #include <map> #include <cmath> #include ...

  3. JSP 页面显示sql中数据。el

    存储数据库字段. package Bean; /** * Created by Administrator on 2017/5/24. */ public class info { private S ...

  4. Python全栈之路----函数----作用域

    Python中,一个函数就是一个作用域. 局部变量放置在其作用域中,根据作用域来区分,函数属于你,函数属于我. 定义完成后,作用域已经生成,使用时顺着作用域链向上查找. 函数定义完成后,不管被在哪儿被 ...

  5. yaf nginx 设置

    #test1server { listen 80; listen [::]:80; root /vagrant_data/aaa/public; index index.html index.htm ...

  6. Arch Linux 的休眠设置

    https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate_(简体中文)https://wiki.archl ...

  7. cmake编译obs

    https://blog.csdn.net/su_vast/article/details/74984213 https://blog.csdn.net/u011258240/article/deta ...

  8. Jenkins入门-环境搭建(1)

    因为Jenkins的环境搭建比较简单,本来不想来介绍,但是发现有些入门小朋友,从各种网站上下载的各种安装包来搭建,最后导致出现了各种千奇百怪的问题,介于这种情况下我决定还是来写一下Jenkins的环境 ...

  9. json转对象,奇怪的映射

    偶然看见此代码,记录下,将来可能会用到. ObjectMapper objectMapper = new ObjectMapper(); if (StringUtils.isNotEmpty(vari ...

  10. spring boot通过Interceptor和HandlerMethodReturnValueHandler实现统一处理为controller返回对象统计处理时间

    思路:实现思路都是基于Aop实现,方式上可以通过spring aop和spring mvc的aop机制都能实现. 通过Interceptor的可以实现为controller插入开始时间和执行结束时间, ...