Hibernate5二级缓存

一级缓存:

Hibernate默认是开启一级缓存的,一级缓存存放在session上,属于事务级数据缓冲。

二级缓存:

二级缓存是在SessionFactory,所有的Session共享同一个二级Cache。二级Cache的内部如何实现并不重要,重要的是采用哪种正确的缓存策略,以及采用哪个Cache提供器。

二级缓存也分为了两种:

内置缓存:Hibernate自带的,不可卸载,通常在Hibernate的初始化阶段,Hibernate会把映射元数据和提前定义的SQL语句放置到SessionFactory的缓存中。该内置缓存是仅仅是只读的。

外置缓存:通常说的二级缓存也就是外置缓存,在默认情况下SessionFactory不会启用这个缓存插件,外置缓存中的数据是数据库数据的复制,外置缓存的物理介质能够是内存或者硬盘。

二级缓存的配置

hibernate支持的缓存插件

   

适合存放到第二级缓存中?   
1) 很少被修改的数据   
2) 不是很重要的数据,允许出现偶尔并发的数据   
3) 不会被并发访问的数据   
4) 常量数据   
不适合存放到第二级缓存的数据?   
1) 经常被修改的数据   
2) 绝对不允许出现并发访问的数据,如财务数据,绝对不允许出现并发   
3) 与其他应用共享的数据。

1 导入jar包和创建配置文件 ehcache.xml

		<!-- 二级缓存 EHcache -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>5.2.17.Final</version>
		</dependency>

ehcache.xml 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
	<diskStore path="d:/ehcache/"></diskStore>
	<!-- 默认缓存配置 -->
	<defaultCache 
		maxElementsInMemory="10000" 
		eternal="false"
		timeToIdleSeconds="120" 
		timeToLiveSeconds="120" 
		overflowToDisk="true" />
		
</ehcache>

2 在 hibernate.cfg.xml 中开启二级缓存

 <!-- 配置二级缓存 ehCache  -->
  <property name="hibernate.cache.use_second_level_cache">true</property>
  <!-- 配置二级缓存技术提供者 -->
  <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
  
 <!-- ORM 映射关系 -->
  <mapping resource="cn/jq/hibernate5/model/Teacher.hbm.xml"/>
  <mapping resource="cn/jq/hibernate5/model/Student.hbm.xml"/>
 <!-- 配置二级缓存类  也可在xxx.hbm.xml 里配置二级缓存类 --> 
  <class-cache usage="read-write" class="cn.jq.hibernate5.model.Student"/>
  <class-cache usage="read-write" class="cn.jq.hibernate5.model.Teacher"/>

3. 测试

	@Test
	public void test() {
		Student student = session.get(Student.class, 2);
		System.out.println(student);
		
		transaction.commit();
		session.close();
		
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		Student student2 = session.get(Student.class, 2);
		System.out.println(student2);
		
	}

session查询一次: session关闭后,不会发起sql查询, 在二级缓存中提取数据

Student [id=2, sname=学生2, teacher=Teacher [id=1, tname=老师1]]
Student [id=2, sname=学生2, teacher=Teacher [id=1, tname=老师1]]

HibernateUtil 工具类的封装

HibernateUtil :

//获取HIbernate与数据库的连接session会话对象, 此工具类采取单例模式
public class HibernateUtil {

	private static HibernateUtil hibernateUtil = null;
	
	private HibernateUtil() {}
	
	public static HibernateUtil getInstance() {
		if(hibernateUtil == null) {
			hibernateUtil = new HibernateUtil();
		}
		return hibernateUtil;
	}
	
	public SessionFactory getSessionFactory() {
		// 配置类: 封装有我们的配置文件里的配置信息, 返回的 configuration 包含有配置文件里的具体信息
		Configuration configuration = new Configuration().configure();
		// Hibernate5规定: 所有配置或服务要生效, 必须将其注册到一个服务注册类中
		StandardServiceRegistry serviceRegistry = configuration.getStandardServiceRegistryBuilder().build();
		//
		SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
		return sessionFactory;
	}
	
	public Session getSession() {
		//获取与当前线程绑定的session,同时,在核心配置文件中配置Session的管理方式为thread线程管理方式:
		//<property name="current_session_context_class">thread</property>
		return getSessionFactory().getCurrentSession();
	}
}
<property name="current_session_context_class">thread</property>  

Dao:

public class StudentDao {
	public void save(Student student) {
		Session session = HibernateUtil.getInstance().getSession();
		
		Transaction transaction = session.beginTransaction();
		session.save(student);
		transaction.commit();
		//thread线程方式来管理session对象, 当事务提交时, Hibernate会自动关闭session, 
		System.out.println(session.isOpen()); //false
	}
}

Test:

public class Test {
	public static void main(String[] args) {
		StudentService studentService = new StudentService();
		Student student = new Student();
		student.setSname("HibernteUtil 测试");

		studentService.save(student);
	}
}

-------
Hibernate: 
    insert 
    into
        t_student
        (sname, teacher_id) 
    values
        (?, ?)
false

参考文章: 

https://www.cnblogs.com/xiaoluo501395377/p/3377604.html

https://blog.csdn.net/luckyzhoustar/article/details/47748179

猜你喜欢

转载自blog.csdn.net/qq_42402854/article/details/81749186