20. 二级缓存(SessionFactory级缓存)

hibernate二级缓存的重要知识点

 

二级缓存也称进程级的缓存或SessionFactory级的缓存,二级缓存可以被所有的session共享

二级缓存的生命周期和SessionFactory的生命周期一致,SessionFactory可以管理二级缓存

 

hibernate提供了基于Hashtable的二级缓存实现,但建议只用于开发阶段,生产环境中应使用第三方提供的缓存产品

 

二级缓存的配置和使用:(本示例基于第三方产品echcache)

       * {hibernate_home}/etc/echcache.xml文件拷贝到src

       * 开启二级缓存,修改hibernate.cfg.xml文件

              <property name="hibernate.cache.use_second_level_cache">true</property>

       * 指定缓存产品提供商,修改hibernate.cfg.xml文件

              <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

       * 指定那些实体类使用二级缓存(两种方法)

              * 在映射文件中采用<cache>标签

              * hibernate.cfg.xml文件中,采用<class-cache>标签

             

二级缓存是缓存实体对象的

 

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

 

 

 

1.配置echcache.xml文件

echcache.xml文件放置到src目录下,同hibernate.cfg.xml一级目录

2.修改hibernate.cfg.xml文件

<!DOCTYPE hibernate-configuration PUBLIC

       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

       "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

       <session-factory>

              <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_cache</property>

                   <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

              <property name="hibernate.connection.username">root</property>

              <property name="hibernate.connection.password">bjsxt</property>

              <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

              <property name="hibernate.show_sql">true</property>

             

              <!-- 开启二级缓存,默认就是开启的 -->

              <property name="hibernate.cache.use_second_level_cache">true</property>

             

              <!-- 指定缓存产品提供商,本示例使用echcache -->

<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

             

              <mapping resource="com/bjsxt/hibernate/Classes.hbm.xml"/>

              <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>

              <!--指定使用两级缓存的实体类,也可以在类映射文件中配置-->

              <class-cache class="com.bjsxt.hibernate.Student" usage="read-only"/>

       </session-factory>

</hibernate-configuration>

3. 编写测试用例

 

 

 

package com.bjsxt.hibernate;

 

import java.io.Serializable;

import org.hibernate.CacheMode;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import junit.framework.TestCase;

 

public class CacheLevel2Test 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();

                    

                     //不会发出sql,因为开启了二级缓存,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,分别调用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();

                    

                     //不会发出sql,因为开启了二级缓存,session是共享二级缓存的

                     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,在使用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);

              }

             

              //管理二级缓存

              SessionFactory factory = HibernateUtils.getSessionFactory();

              //factory.evict(Student.class);

              factory.evict(Student.class, 1);

             

              try {

                     session = HibernateUtils.getSession();

                     session.beginTransaction();

                    

                     //会发出查询sql,因为二级缓存中的数据被清除了

                     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.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);

              }

             

              try {

                     session = HibernateUtils.getSession();

                     session.beginTransaction();

                    

                     //发出sql语句,因为session设置了CacheModeGET,所以二级缓存中没有数据

                     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();

                    

                     //只向二级缓存写数据,而不从二级缓存读数据

                     session.setCacheMode(CacheMode.PUT);

                    

                     //会发出查询sql,因为sessionCacheMode设置成了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、付费专栏及课程。

余额充值