hibernate 二级缓存

package com.qjmotor.hibernate;


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

import junit.framework.TestCase;

public class CacheTest extends TestCase {
 
 
 /**
  * 开启二级缓存
  *
  * 两个session中,缓存数据
  */
 public void testCache1(){
  Session session=null;
  try{
   session=HibernateUtils.getSession();
   session.beginTransaction();
   //发出Sql语句
   Student student = (Student)session.get(Student.class, 1);
   System.out.println("student.getName()=" + student.getName());
   
   session.getTransaction().commit();
  }catch(Exception e){
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally{
   HibernateUtils.closeSession(session);
  }
  
  try{
   session=HibernateUtils.getSession();
   session.beginTransaction();
   //不会发出查询语句,二级缓存是进程级的,不同session共享二级缓存
   Student student = (Student)session.get(Student.class, 1);
   System.out.println("student.getName()=" + student.getName());
   
   session.getTransaction().commit();
  }catch(Exception e){
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally{
   HibernateUtils.closeSession(session);
  }
 }
 
 /**
  * 开启二级缓存
  *
  * 两个session中,缓存数据
  */
 public void testCache2(){
  Session session=null;
  try{
   session=HibernateUtils.getSession();
   session.beginTransaction();
   //发出Sql语句
   Student student = (Student)session.load(Student.class, 1);
   System.out.println("student.getName()=" + student.getName());
   
   session.getTransaction().commit();
  }catch(Exception e){
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally{
   HibernateUtils.closeSession(session);
  }
  
  try{
   session=HibernateUtils.getSession();
   session.beginTransaction();
   //不会发出查询语句,二级缓存是进程级的,不同session共享二级缓存
   Student student = (Student)session.load(Student.class, 1);
   System.out.println("student.getName()=" + student.getName());
   
   session.getTransaction().commit();
  }catch(Exception e){
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally{
   HibernateUtils.closeSession(session);
  }
 }

}

 

 

 

 

testCache1()结果

14:45:06,593  WARN CacheFactory:43 - read-only cache configured for mutable class: com.qjmotor.hibernate.Student
14:45:06,593  WARN EhCacheProvider:86 - Could not find configuration [com.qjmotor.hibernate.Student]; using defaults.
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from t_student student0_ where student0_.id=?
student.getName()=李四
student.getName()=李四

到此处说明不同session是共享二级缓存的

 

换种测试方式,testCache1()在第二个try出设置断点,目的是待student装入二级缓存后,执行testCache2()

testCache2()结果

Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from t_student student0_ where student0_.id=?
student.getName()=李四
student.getName()=李四

 

为什么testCache2()不利用二级缓存取数据呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值