HIbernate的一级缓存和快照

什么是Hibernate的一级缓存

       Hibernate的一级缓存就是指Session缓存,Session缓存是一块内存空间,用来存放相互管理的java对象,在使用Hibernate查询对象的时候,首先使用对象属性的OID值在Hibernate的一级缓存种进行查找,如果找到匹配OID值的对象,就直接将该对象从一级缓存种取出使用,不会再查询数据库;如果没有找到相同OID值的对象,则会去数据库中查找相应数据。当从数据库中查询到所需数据时,该数据信息也会放置到一级缓存中。 H i b e r n a t e 的 一 级 缓 存 的 作 用 就 是 减 少 对 数 据 库 的 访 问 次 数 。 \color{red}{Hibernate的一级缓存的作用就是减少对数据库的访问次数。} Hibernate访

       在Session接口的实现中包含 一 系 列 的 J a v a 集 合 , \color{red}{一系列的Java集合,} Java这些Java集合构成了Session缓存。只要Session实例没有结束生命周期,存放在它缓存中的对象也不会结束生命周期。所以一级缓存也被称为是Session基本的缓存。

Hibernate的一级缓存有如下特点:

  • 当应用程序调用Session接口的save()、update()、saveOrUpdate()时,如果Session缓存中没有相应的对象,Hibernate就会自动的把从数据库中查询到的相应对象信息加入到一级缓存中去。
  • 当调用Session接口的load()、get()方法,一级Query接口的list()、iterator()方法时,会判断缓存中是否存在该对象,有则返回,不会查询数据库,如果缓存中没有要查询的对象,再去数据库中查询对应对象,并添加到一级缓存中。
  • 当调用Session的close()方法时,Session缓存会被清空。
测试一级缓存

配置orm元数据:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itheima.domain" >
	<class name="Customer" table="cst_customer" >
		<id name="cust_id"  >
			<generator class="increment"></generator>
		</id>
		<property name="cust_name" column="cust_name" ></property>
		<property name="cust_source" column="cust_source" ></property>
		<property name="cust_industry" column="cust_industry" ></property>
		<property name="cust_level" column="cust_level" ></property>
		<property name="cust_linkman" column="cust_linkman" ></property>
		<property name="cust_phone" column="cust_phone" ></property>
		<property name="cust_mobile" column="cust_mobile" ></property>
	</class>
</hibernate-mapping>

测试方法:

package pers.zhang.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import pers.zhang.domain.Customer;

//测试一级缓存
public class Demo {

	@Test
	//证明一级缓存存在
	public void fun1(){
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer c1 = session.get(Customer.class, 1l);
		Customer c2 = session.get(Customer.class, 1l);
		Customer c3 = session.get(Customer.class, 1l);
		Customer c4 = session.get(Customer.class, 1l);
		Customer c5 = session.get(Customer.class, 1l);
		
		System.out.println(c3==c5);//true

		tx.commit();
		session.close();
	}
}

运行JUnit测试输出:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?
true

在以上代码中,第一次执行Session的get()方法获取customer对象时,由于一级缓存中没有数据,所以Hibernate会向数据库发送一条SQL语句,查询id等于1的对象;
当再次调用了Session的get()方法获取customer2对象时,将不会再发送sql语句,这是因为customer2对象是从一级缓存中获取的。
缓存原理如下图:
在这里插入图片描述

一级缓存的内部结构:(快照区)

Customer表如下:
在这里插入图片描述
测试方法:

@Test
	//
	public void fun2(){
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		Customer c1 = session.get(Customer.class, 1l);
		
		c1.setCust_name("百度");//第一次修改客户名
		c1.setCust_name("Google");//第二次改回原来的Google
		
		tx.commit();
		session.close();
	}

运行JUnit测试输出:

Hibernate: 
    select
        customer0_.cust_id as cust_id1_0_0_,
        customer0_.cust_name as cust_nam2_0_0_,
        customer0_.cust_source as cust_sou3_0_0_,
        customer0_.cust_industry as cust_ind4_0_0_,
        customer0_.cust_level as cust_lev5_0_0_,
        customer0_.cust_linkman as cust_lin6_0_0_,
        customer0_.cust_phone as cust_pho7_0_0_,
        customer0_.cust_mobile as cust_mob8_0_0_ 
    from
        cst_customer customer0_ 
    where
        customer0_.cust_id=?

为何我们修改了数据却没有打印update的sql语句?因为快照的存在。

在这里插入图片描述

Hibernate向一级缓存放入数据时,同时复制一份数据放入到Hibernate快照中,当使用commit()方法提交事务时,同时会清理Session的一级缓存, 这 时 会 使 用 O I D 判 断 一 级 缓 存 中 的 对 象 和 快 照 中 的 对 象 是 否 一 致 , 如 果 两 个 对 象 中 的 属 性 发 生 变 化 , 则 执 行 u p d a t e 语 句 , 将 缓 存 的 内 容 同 步 到 数 据 库 中 , 并 更 新 快 照 ; 如 果 一 致 , 则 不 执 行 u p d a t e 语 句 。 \color{red}{这时会使用OID判断一级缓存中的对象和快照中的对象是否一致,如果两个对象中的属性发生变化,则执行update语句,将缓存的内容同步到数据库中,并更新快照;如果一致,则不执行update语句。} 使OIDupdateupdateHibernate快照的作用就是确保一级缓存中的数据和数据库中的数据一致。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值