Hibernate二级缓存

package myHibernate;

import java.io.Serializable;

import myHibernate.HibernateUtils;
import myHibernate.Student;

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

import junit.framework.TestCase;

public class CacheLevel2Test extends TestCase {

    /**
     * 开启个session,分别调用load
     * 先将二级缓存设为false,也就是只使用session级的缓存,即不能跨session访问缓存,
     * 所以会发出两次SQL语句;
     * 然后将二级缓存设为true,可以debug观察发现只发出了一次SQL语句
     */
    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);
        }
        //第二个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);
        }
    }   
    //用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);
        }
        //第二个session
        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);
        }
    }   
/**
 *  开启个session,分别调用load,第一个调用完之后便使用evict管理二级缓存,
 *  evict可以将某个类的所有缓存都清除,也可以只清除某一个对象,这时候要指明
 *  id
 * */
    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);
        }
        SessionFactory sf = HibernateUtils.getSessionFactory();
        //清除该类的所有对象
        //sf.evict(Student.class);
        //清除某个特定对象
        sf.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);
        }
    }
   
    /**
     * session与二级缓存的交互
     * */
    public void testCache4() {
        Session session = null;
//第一个session   
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            /*
             * 设置CacheMode
             * CacheMode.NORMAL :从二级缓存中读、写数据
             * CacheMode.GET : 从二级缓存中读取数据,仅在数据更新时对二级缓存写数据
             * CacheMode.PUT : 仅向二级缓存写数据,但不从二级缓存度数据
             * */
            System.out.println("第一个session");
            session.setCacheMode(CacheMode.GET);
            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);
        }
        SessionFactory sf = HibernateUtils.getSessionFactory();

//第二个session,不设置CacheMode,为默认状态,即会读、写缓存   
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            //会发出SQL语句,因为第一个session设置CacheMode.GET
            System.out.println("第二个session");
            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);
        }
//第三个session,不设置CacheMode
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            System.out.println("第三个session");
            //不会发错SQL语句,因为因为第二个session没有设置CacheMode,则缓存里有数据
            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);
        }
//第四个session    ,将CacheMode设置为PUT,即只写不读       
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            System.out.println("第四个session");
            //会发出SQL语句,因为CacheMode设置为PUT,虽然缓存里有数据但不读取
            session.setCacheMode(CacheMode.PUT);
            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);
        }
    }
   
   
   
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值