Hibernate 缓存 - 一级缓存
欢迎来到 Hibernate 缓存 - 一级缓存示例教程。最近我们研究了Hibernate 架构、Hibernate 映射以及如何使用HQL以面向对象的方式触发 SQL 查询。今天我们将研究 Hibernate 的一个重要方面 - Hibernate 缓存。
Hibernate缓存
如果使用得当,Hibernate Cache 对于提高应用程序性能非常有用。缓存的理念是减少数据库查询的数量,从而减少应用程序的吞吐时间。Hibernate 带有不同类型的缓存:
- 一级缓存:Hibernate 一级缓存与 Session 对象相关联。默认情况下,Hibernate 一级缓存处于启用状态,无法禁用。但是,Hibernate 提供了一些方法,通过这些方法我们可以从缓存中删除选定的对象或完全清除缓存。会话中缓存的任何对象对其他会话都是不可见的,并且当会话关闭时,所有缓存的对象也将丢失。
- 二级缓存:默认情况下,Hibernate 二级缓存是禁用的,但我们可以通过配置启用它。目前,EHCache 和 Infinispan 为 Hibernate 二级缓存提供了实现,我们可以使用它们。我们将在下一个有关 Hibernate 缓存的教程中讨论这一点。
- 查询缓存:Hibernate 还可以缓存查询的结果集。Hibernate 查询缓存不会缓存缓存中实际实体的状态;它仅缓存标识符值和值类型的结果。因此,它应始终与二级缓存结合使用。
Hibernate 缓存 - 一级缓存示例
对于我的 Hibernate 一级缓存示例程序,我使用与HQL 示例中相同的配置,您可以查看并配置表并用虚拟数据填充它。让我们首先查看程序及其输出,然后我们将介绍一些与 Hibernate 一级缓存相关的要点。HibernateCacheExample.java
package com.journaldev.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.journaldev.hibernate.model.Employee;
import com.journaldev.hibernate.util.HibernateUtil;
public class HibernateCacheExample {
public static void main(String[] args) throws InterruptedException {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
//Get employee with id=1
Employee emp = (Employee) session.load(Employee.class, new Long(1));
printData(emp,1);
//waiting for sometime to change the data in backend
Thread.sleep(10000);
//Fetch same data again, check logs that no query fired
Employee emp1 = (Employee) session.load(Employee.class, new Long(1));
printData(emp1,2);
//Create new session
Session newSession = sessionFactory.openSession();
//Get employee with id=1, notice the logs for query
Employee emp2 = (Employee) newSession.load(Employee.class, new Long(1));
printData(emp2,3);
//START: evict example to remove specific object from hibernate first level cache
//Get employee with id=2, first time hence query in logs
Employee emp3 = (Employee) session.load(Employee.class, new Long(2));
printData(emp3,4);
//evict the employee object with id=1
session.evict(emp);
System.out.println("Session Contains Employee with id=1?"+session.contains(emp));
//since object is removed from first level cache, you will see query in logs
Employee emp4 = (Employee) session.load(Employee.class, new Long(1));
printData(emp4,5);
//this object is still present, so you won't see query in logs
Employee emp5 = (Employee) session.load(Employee.class, new Long(2));
printData(emp5,6);
//END: evict example
//START: clear example to remove everything from first level cache
session.clear();
Employee emp6 = (Employee) session.load(Employee.class, new Long(1));
printData(emp6,7);
Employee emp7 = (Employee) session.load(Employee.class, new Long(2));
printData(emp7,8);
System.out.println("Session Contains Employee with id=2?"+session.contains(emp7));
tx.commit();
sessionFactory.close();
}
private static void printData(Employee emp, int count) {
System.out.println(count+":: Name="+emp.getName()+", Zipcode="+emp.getAddress().getZipcode());
}
}
当我们运行上述示例时,输出包含大量与 Hibernate 相关的信息。但我们最感兴趣的是我们的代码特定输出以及 Hibernate 为加载数据而触发的查询。输出片段如下所示。
Hibernate Configuration loaded
Hibernate serviceRegistry created
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
1:: Name=Pankaj, Zipcode=95129
2:: Name=Pankaj, Zipcode=95129
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
3:: Name=PankajK, Zipcode=95129
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
4:: Name=David, Zipcode=95051
Session Contains Employee with id=1?false
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
5:: Name=Pankaj, Zipcode=95129
6:: Name=David, Zipcode=95051
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
7:: Name=Pankaj, Zipcode=95129
Hibernate: select employee0_.emp_id as emp_id1_1_0_, employee0_.emp_name as emp_name2_1_0_, employee0_.emp_salary as emp_sala3_1_0_, address1_.emp_id as emp_id1_0_1_, address1_.address_line1 as address_2_0_1_, address1_.city as city3_0_1_, address1_.zipcode as zipcode4_0_1_ from EMPLOYEE employee0_ left outer join ADDRESS address1_ on employee0_.emp_id=address1_.emp_id where employee0_.emp_id=?
8:: Name=David, Zipcode=95051
Session Contains Employee with id=2?true
Hibernate 中的一级缓存重点
从上述程序中可以得出有关 Hibernate 中一级缓存的重要点如下:
- Hibernate 一级缓存默认启用,不需要进行任何配置。
- Hibernate 一级缓存是会话特定的,这就是为什么当我们在同一个会话中获取相同的数据时不会触发查询,而在其他会话中会触发查询来加载数据。
- Hibernate 一级缓存可以有旧值,如上所示,我让程序休眠了 10 秒,在此期间我更新了数据库中的值(名称从 Pankaj 改为 PankajK),但该值并未反映在同一会话中。但在其他会话中,我们获得了更新的值。
- 我们可以使用会话
evict()
方法从 Hibernate 一级缓存中删除单个对象。 - 我们可以使用会话
clear()
方法来清除缓存,即从缓存中删除所有对象。 - 我们可以使用 session
contains()
方法来检查某个对象是否存在于 Hibernate 缓存中,如果在缓存中找到该对象,则返回 true,否则返回 false。 - 由于 Hibernate 将所有对象缓存到会话一级缓存中,因此在运行批量查询或批量更新时,需要按照一定时间间隔清除缓存以避免内存问题。
这就是 Hibernate 缓存和一级缓存的示例,在以后的文章中,我们将研究Hibernate 二级缓存 - EHCache实现。