Hibernate 二级缓存

hibernate二级缓存

二级缓存也称为进程级的缓存或SessionFactory级的缓存,二级缓存可以被所有的session共享
二级缓存的生命周期和SessionFactory的生命周期一致,SessionFactory可以管理二级缓存

二级缓存的配置和使用:
 * 将ehcache.xml文件拷贝到src下
 * 在hibernate.cfg.xml文件中加入缓存产品提供商
 <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
 * 启用二级缓存,这也是它的默认配置
 <property name="hibernate.cache.use_second_level_cache">true</property>
 * 指定哪些实体类使用二级缓存
 可以在映射文件中采用<cache>标签指定或在hibernate.cfg.xml文件中统一指定
 注意使用的策略,通常采用read-only和read-write
 缓存原则:通常读远远大于写的数据进行缓存
 
二级缓存主要是缓存实体对象的

了解一级缓存和二级缓存的交互

注意大批量数据更新时,如果配置了二级缓存建议禁用一级缓存和二级缓存的交互

 

import org.hibernate.CacheMode;
import org.hibernate.Session;

import junit.framework.TestCase;

public class CacheTest extends TestCase {

 /**
  * 开启二级缓存
  *
  * 在两个session中发load查询
  */
 public void testCache1() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Student student = (Student)session.load(Student.class, 1);
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Student student = (Student)session.load(Student.class, 1);
   
   //不会发出查询语句,因为配置二级缓存,session可以共享二级缓存中的数据
   //二级缓存是进程级的缓存
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
 }  
 
 /**
  * 开启二级缓存
  * 
  * 在两个session中发get查询
  */
 public void testCache2() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Student student = (Student)session.get(Student.class, 1);
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Student student = (Student)session.get(Student.class, 1);
   
   //不会发出查询语句,因为配置二级缓存,session可以共享二级缓存中的数据
   //二级缓存是进程级的缓存
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
 } 
 
 /**
  * 开启二级缓存
  * 
  * 在两个session中发load查询,采用SessionFactory管理二级缓存
  */
 public void testCache3() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Student student = (Student)session.load(Student.class, 1);
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  //管理二级缓存
  //HibernateUtils.getSessionFactory().evict(Student.class);
  HibernateUtils.getSessionFactory().evict(Student.class, 1);
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Student student = (Student)session.load(Student.class, 1);
   
   //会发出查询语句,因为二级缓存中的数据被清除了
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }
 
 
 /**
  * 开启二级缓存
  * 
  * 一级缓存和二级缓存的交互
  */
 public void testCache4() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   //禁止将一级缓存中的数据放到二级缓存中
   session.setCacheMode(CacheMode.IGNORE);
   Student student = (Student)session.load(Student.class, 1);
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }

  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   Student student = (Student)session.load(Student.class, 1);
   
   //会发出查询语句,因为禁止了一级缓存和二级缓存的交互
   System.out.println("student.name=" + student.getName());
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }
 
 /**
  * 大批量的数据添加
  */
 public void testCache5() {
  Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   //禁止一级缓存和二级缓存交互
   session.setCacheMode(CacheMode.IGNORE);
   for (int i=0; i<100; i++) {
    Student student = new Student();
    student.setName("张三" + i);
    session.save(student);
    //每20条更新一次
    if (i % 20 == 0) {
     session.flush();
     //清除缓存的内容
     session.clear();
    }
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
 }   
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值