Hibernate一级缓存与对象之间的关系
Hibernate的一级缓存
Hibernate的一级缓存是指Session缓存,Session缓存时一块内存空间,用来存放相互管理的Java对象,在使用Hibernate查询对象的时候,首先会使用对象属性的OID值在Hibernate的一级缓存中进行查找,如果找到匹配OID值的对象,就直接将该对象从一级缓存中取出使用,不会再查询数据库;如果没有找到相同OID值的对象,则会去数据库中查找相应数据。当从数据库中查询到所需数据时,该数据信息也会放置到一级缓存中,Hibernate一级缓存的作用就是减少对数据库的访问次数。
Hibernate的一级缓存会有如下特点:
- 当应用程序调用Session接口的save() update() saveOrUpdate() load() get()方法以及以及Query接口的list() iterator()方法时,如果session缓存中没有相应的对象,Hibernate就会自动的把从数据库中查询到的相应对象信息加入到一级缓存中。
- 当调用Session的close()方法时,Session缓存会被清空。
示例代码:
1 public class Demo1 { 2 @Test 3 public void func1(){ 4 Configuration configure=new Configuration().configure(); 5 SessionFactory sf=configure.buildSessionFactory(); 6 Session session=sf.openSession(); 7 Transaction transaction=session.beginTransaction(); 8 Customer c1= (Customer) session.get(Customer.class,"4028e48d604887ef01604887f2450000"); 9 Customer c2= (Customer) session.get(Customer.class,"4028e48d604887ef01604887f2450000"); 10 Customer c3= (Customer) session.get(Customer.class,"4028e48d604887ef01604887f2450000"); 11 transaction.commit(); 12 session.close(); 13 sf.close(); 14 } 15 }
应用程序打印输出结果:只会打印一次select语句。
1 Hibernate: 2 select 3 customer0_.cust_id as cust1_0_0_, 4 customer0_.cust_name as cust2_0_0_, 5 customer0_.cust_source as cust3_0_0_, 6 customer0_.cust_industry as cust4_0_0_, 7 customer0_.cust_level as cust5_0_0_, 8 customer0_.cust_linkman as cust6_0_0_, 9 customer0_.cust_phone as cust7_0_0_, 10 customer0_.cust_mobile as cust8_0_0_ 11 from 12 CUST customer0_ 13 where 14 customer0_.cust_id=?
缓存原理

- Java程序调用get查询缓存,先从缓存中查看是否存在查询的OID的对象,如果有,则直接返回到缓存中。
- 如果没有则发送sql语句到数据库进行查询
- 数据库将查询到的内容封装到ResultSet中返回。
- 将resultSet中的内容组装程查询的对象
- 将查询到的对象存入session缓存
- 将对象返回程序
Hibernate缓存之快照

- Java程序调用get查询缓存,先从缓存中查看是否存在查询的OID的对象,如果有,则直接返回到缓存中。
- 如果没有则发送sql语句到数据库进行查询
- 数据库将查询到的内容封装到ResultSet中返回。
- 将resultSet中的内容组装程查询的对象,注意这里是组装成了两个对象,
- 将组装好的对象一个放入缓存中,一个放入快照中。
- 将缓存对象返回给程序
- 程序对返回的缓存对象进行修改
- 事物提交
- hibernate比对缓存中的对象和快照,如果有变化会同步到数据库中,如果没有变化则对数据库中的数据不做修改。
示例:当缓存对象与快照不一致时:
1 public class Demo1 { 2 @Test 3 public void func1(){ 4 Configuration configure=new Configuration().configure(); 5 SessionFactory sf=configure.buildSessionFactory(); 6 Session session=sf.openSession(); 7 Transaction transaction=session.beginTransaction(); 8 Customer c1= (Customer) session.get(Customer.class,"4028e48d604887ef01604887f2450000"); 9 c1.setCust_name("zhangsan"); 10 transaction.commit(); 11 session.close(); 12 sf.close(); 13 } 14 }
程序打印输出为:
1 Hibernate: 2 select 3 customer0_.cust_id as cust1_0_0_, 4 customer0_.cust_name as cust2_0_0_, 5 customer0_.cust_source as cust3_0_0_, 6 customer0_.cust_industry as cust4_0_0_, 7 customer0_.cust_level as cust5_0_0_, 8 customer0_.cust_linkman as cust6_0_0_, 9 customer0_.cust_phone as cust7_0_0_, 10 customer0_.cust_mobile as cust8_0_0_ 11 from 12 CUST customer0_ 13 where 14 customer0_.cust_id=? 15 Hibernate: 16 update 17 CUST 18 set 19 cust_name=?, 20 cust_source=?, 21 cust_industry=?, 22 cust_level=?, 23 cust_linkman=?, 24 cust_phone=?, 25 cust_mobile=? 26 where 27 cust_id=?
当修改之后的缓存对象与数据库中的数据一致时:
1 public class Demo1 { 2 @Test 3 public void func1(){ 4 Configuration configure=new Configuration().configure(); 5 SessionFactory sf=configure.buildSessionFactory(); 6 Session session=sf.openSession(); 7 Transaction transaction=session.beginTransaction(); 8 Customer c1= (Customer) session.get(Customer.class,"4028e48d604887ef01604887f2450000"); 9 c1.setCust_name("zhangsan"); 10 c1.setCust_name("Zah"); 11 transaction.commit(); 12 session.close(); 13 sf.close(); 14 } 15 }
即将数据库中的数据先改为“zhangsan”,又改为原来的值时,发现程序打印输出的结果并没有执行update语句。
1 Hibernate: 2 select 3 customer0_.cust_id as cust1_0_0_, 4 customer0_.cust_name as cust2_0_0_, 5 customer0_.cust_source as cust3_0_0_, 6 customer0_.cust_industry as cust4_0_0_, 7 customer0_.cust_level as cust5_0_0_, 8 customer0_.cust_linkman as cust6_0_0_, 9 customer0_.cust_phone as cust7_0_0_, 10 customer0_.cust_mobile as cust8_0_0_ 11 from 12 CUST customer0_ 13 where 14 customer0_.cust_id=?
所以hibernate提高效率有两种手段:1.使用缓存,减少不必要的查询;2.使用快照,减少不必要的更新操作。
Session一级缓存和对象状态之间的关系:
之前提到过的与session有关联,即为该对象存储在session缓存中,所以:
- 瞬时态:没有id,没有存储在session缓存中
- 持久态:有id,存储在session缓存中
- 游离态:有id,没有存储在session缓存中
1 public class Demo1 { 2 @Test 3 public void func1(){ 4 Configuration configure=new Configuration().configure(); 5 SessionFactory sf=configure.buildSessionFactory(); 6 Session session=sf.openSession(); 7 Transaction transaction=session.beginTransaction(); 8 Customer c=new Customer(); 9 c.setCust_id("4028e48d604887ef01604887f2450000");//设置id,由新建状态转换为托管状态 10 session.update(c);//将对象放入session缓存中 11 Customer c1= (Customer) session.get(Customer.class,"4028e48d604887ef01604887f2450000");//从缓存中取对象 12 transaction.commit();//快照和缓存进行对比,如果不一致则提交数据库 13 session.close(); 14 sf.close(); 15 } 16 }
打断点测试只有在事务提交的时候,才会打印update语句。

浙公网安备 33010602011771号