hibernate的二级缓存

简介:
      hibernate二级缓存是由SessionFactory管理,所以又叫SessionFactory级缓存,它是通过不同的类库来实现的,比如ehcache、oscache等。和一级缓存一样,二级缓存也是用来缓存实体对象的,对普通属性不缓存。
      hibernate二级缓存的使用需要进行必要的配置,主要是四个地方(这里以ehcache为例):
      1>. 配置echcache.xml文件
      2>.开启二级缓存,修改hibernate.cfg.xml文件
         <property name="hibernate.cache.use_second_level_cache">true</property>
      3>.指定缓存产品提供商,修改hibernate.cfg.xml文件
         <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
      4>.指定那些实体类使用二级缓存(两种方法)
           1).在映射文件中采用<cache>标签
           2).在hibernate.cfg.xml文件中,采用<class-cache>标签
      hibernate二级缓存配置上之后,就成了“客观存在”,hibernate在使用某些方法的时候默认就使用和维护了二级缓存(哪怕你出于某种原因希望使用也不行)。因此,在使用二级缓存时进行一定的控制还是必要的,Session就提供了设置使用二级缓存的模式的方法(setCacheMode)来实现,当session调用某个方法时对二级缓存的存取改变。
1.实体类:
   Student.java
public  class Student { 
   private Integer id; 
   private String name; 
   //一系列的setter.getter方法 
}
 
2.映射文件:
   Student.hbm.xml
   < class  name ="com.sxt.hibernate.cache.entity.Student"  table ="sxt_hibernate_student" > 
         
    <!--  指定本类的对象使用二级缓存(这也可以放在hibernate.cfg.xml中统一指定) --> 
    <!--  
    <cache usage="read-only"/> 
     
--> 
     < id  name ="id"  length ="4" > 
       < generator  class ="native" > </ generator > 
     </ id > 
     < property  name ="name"  length ="10" > </ property > 
   </ class >
 
3. 二级缓存配置文件:
  ehcache.xml
< ehcache > 
  <!--  当二级缓存溢出时,对象保存的临时磁盘路径 --> 
         < diskStore  path ="java.io.tmpdir" /> 

        <!-- name="sampleCache2" 缓存名字 
                maxElementsInMemory="1000" 缓存里可存放的最大对象数. 
                eternal="true" 缓存对象是否永久有效(true表示是). 
                timeToIdleSeconds="120" 对象在缓存中存活的空闲时间,即空闲多久它就失效,单位是秒. 
                timeToLiveSeconds="120" 对象在缓存中存活的时间,单位是秒. 
                overflowToDisk="true"    当缓存溢出时,对象是否保存到磁盘上.保存的磁盘路径由<diskStore>中的path指定. 
        
--> 
         < defaultCache 
                 maxElementsInMemory ="10000" 
                 eternal ="false" 
                 timeToIdleSeconds ="120" 
                 timeToLiveSeconds ="120" 
                 overflowToDisk ="true" 
                 /> 
</ ehcache >
 
4.hibernate配置文件
  hibernate.cfg.xml
< hibernate-configuration > 
   < session-factory > 
     < property  name ="hibernate.connection.url" >jdbc:oracle:thin:@localhost:1521:ORCL10 </ property > 
     < property  name ="hibernate.connection.driver_class" >oracle.jdbc.driver.OracleDriver </ property > 
     < property  name ="hibernate.connection.username" >scott </ property > 
     < property  name ="hibernate.connection.password" >yf123 </ property > 
     < property  name ="hibernate.dialect" >org.hibernate.dialect.Oracle9Dialect </ property > 
     < property  name ="hibernate.show_sql" >true </ property > 
     
    <!--  开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 --> 
     < property  name ="hibernate.cache.use_second_level_cache" >true </ property > 
    <!--  指定二级缓存产品的提供商 --> 
     < property  name ="hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider </ property > 
     
     < mapping  resource ="com/sxt/hibernate/cache/entity/Student.hbm.xml" /> 
     
    <!--  指定那些类使用二级缓存 --> 
     < class-cache  usage ="read-only"  class ="com.sxt.hibernate.cache.entity.Student" /> 
   </ session-factory > 
</ hibernate-configuration >
 
5.测试方法:
  测试一:
   public  static  void main(String[] args) { 
    Session session =  null
    Transaction t =  null

     /** 
     * 开启两个session中发出load查询 
     */
 
/*    try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
      Student student = (Student) session.load(Student.class, 1); 
      System.out.println("student.name=" + student.getName()); 
      t.commit(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    } finally { 
      HibernateUtils.closeSession(session); 
    } 
    try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
      // 不会发出查询语句,因为开启了二级缓存,session共享二级缓存 
      Student student = (Student) session.load(Student.class, 1); 
      System.out.println("student.name=" + student.getName()); 
      t.commit(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    } finally { 
      HibernateUtils.closeSession(session); 
    }*/
 
     
     /** 
     * 开启两个session中发出get查询 
     */
 
     try { 
      session = HibernateUtils.getSession(); 
      System.out.println(session); 
      t = session.beginTransaction(); 
      Student student = (Student) session.get(Student. class, 1); 
      session.clear(); 
      System.out.println( "student.name=" + student.getName()); 
      t.commit(); 
    }  catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    }  finally { 
      HibernateUtils.closeSession(session); 
    } 

     try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
       // 不会发出查询语句,因为开启了二级缓存,get使用二级缓存 
      Student student = (Student) session.get(Student. class, 1); 
      System.out.println( "student.name=" + student.getName()); 
      t.commit(); 
    }  catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    }  finally { 
      HibernateUtils.closeSession(session); 
    } 
  } 
}
  测试二:
   public  static  void main(String[] args) { 
    Session session =  null
    Transaction t =  null

     /** 
     * 开启两个session中发出load查询,使用SessionFactory清除二级缓存 
     */
 
/*    try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
      //load时,会把对象放到两个缓存中 
      Student student = (Student) session.load(Student.class, 1); 
      System.out.println("student.name=" + student.getName()); 
      t.commit(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    } finally { 
      HibernateUtils.closeSession(session); 
    } 
    //用SessionFacotry管理二级缓存 
    SessionFactory factory=HibernateUtils.getSessionFactory(); 
    //evict()把id为1的Student对象从二级缓存中清除. 
    factory.evict(Student.class, 1); 
    try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
      // 不会发出查询语句,因为开启了二级缓存,load也使用二级缓存 
      Student student = (Student) session.load(Student.class, 1); 
      System.out.println("student.name=" + student.getName()); 
      t.commit(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    } finally { 
      HibernateUtils.closeSession(session); 
    }*/
 


     /** 
     * 开启两个session中发出load查询,session对二级缓存的使用. 
     */
 
     try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
       //设置一级缓存和二级缓存的交互模式: 
       //GET表示在load时只是从二级缓存中读取数据,仅在数据更新时对二级缓存写数据。 
       //PUT表示只往二级缓存中放数据,而不从中取数据. 
       //NORMAL表示从二级缓存中读、写数据。 
       //REFRESH表示仅向二级缓存写数据,但不从二级缓存中读数据。 
      session.setCacheMode(CacheMode.GET); 
      Student student = (Student) session.load(Student. class, 1); 
      System.out.println( "student.name=" + student.getName()); 
      t.commit(); 
    }  catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    }  finally { 
      HibernateUtils.closeSession(session); 
    } 
     try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
       // 会发出查询语句,因为前面设置了一级缓存和二级缓存的交互模式为GET,没有往二级缓存中放数据. 
      Student student = (Student) session.load(Student. class, 1); 
      System.out.println( "student.name=" + student.getName()); 
      t.commit(); 
    }  catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    }  finally { 
      HibernateUtils.closeSession(session); 
    } 
     
     try { 
      session = HibernateUtils.getSession(); 
      t = session.beginTransaction(); 
       //设置交互模式为PUT. 
      session.setCacheMode(CacheMode.PUT); 
       // 会发出查询语句,因为设置了一级缓存和二级缓存的交互模式为PUT,只是往二级缓存中放数据,并不去中取数据. 
      Student student = (Student) session.load(Student. class, 1); 
      System.out.println( "student.name=" + student.getName()); 
      t.commit(); 
    }  catch (Exception e) { 
      e.printStackTrace(); 
      t.rollback(); 
    }  finally { 
      HibernateUtils.closeSession(session); 
    } 
  } 
}
 
     本文转自NightWolves 51CTO博客,原文链接:http://blog.51cto.com/yangfei520/279239 ,如需转载请自行联系原作者
相关文章
|
5天前
|
数据采集 自然语言处理 搜索推荐
基于Qwen3的Embedding和Rerank模型系列,开源!
近年来,随着大规模预训练语言模型(LLM)的飞速发展,文本嵌入(Embedding)和重排序(Reranking)技术在搜索引擎、问答系统、推荐系统等多个领域的重要性愈发凸显。
719 72
|
4天前
|
安全 JavaScript 前端开发
Typora免费下载,不需要激活,Typora早期版本,Markdown编辑器
Typora是一款支持实时预览的Markdown编辑器,跨平台兼容Windows、macOS和Linux,适合写作、笔记和技术文档。本文提供合法安全的Typora早期免费版安装方案及常见问题解决方法,助你快速上手。同时详细介绍了Markdown基础用法,包括标题、加粗、斜体、列表、链接、图片、引用、代码块和表格等常用语法,帮助用户高效编写结构化内容。
931 15
|
4天前
|
云安全 人工智能 安全
|
4天前
|
人工智能 前端开发 Devops
通义灵码带你玩转开发者常用的MCP(合辑,持续更新中)
今天我们精选了与开发者息息相关和比较热门的MCP 服务,总结了这些技术服务在实际开发的最佳实践,涵盖了从前端开发、后端开发、DevOps、测试、运维等关键环节,及非研发领域中的热度较高的MCP服务。我们通过直播、图文等形式带你了解和学习!
595 3
|
6天前
|
存储 前端开发 JavaScript
2025 最新前端 100 道经典面试题及详细答案汇总整理
本文整理了100道前端常见面试题及其详细答案,涵盖HTML、CSS、JavaScript等多个领域,助你系统复习前端知识。内容包括HTML5新特性、CSS盒模型、Flex与Grid布局区别、选择器优先级等核心知识点,以及伪类和伪元素的区别等细节。适合准备面试或巩固基础的开发者学习。资源可从文末链接下载。
468 1
|
16天前
|
人工智能 API 开发者
一文带你 "看见" MCP 的过程,彻底理解 MCP 的概念
本文介绍了模型上下文协议(MCP)的基本概念、工作原理及其应用过程。MCP 是一种连接 AI 助手与数据系统的开放标准,旨在帮助大模型生成更高质量的响应。文章从 RAG 和 Function Calling 的理论基础出发,详细解析了 MCP 的核心组件(主机、客户端、服务器)及优势,并通过 ModelScope 和 Cherry Studio 实例展示其操作流程。同时,文中指出了 MCP 存在的手动配置复杂、工具稳定性不足等问题,但也强调其作为过渡协议的重要性,为未来智能体间的协同和工具使用提供了方向。
1592 56
一文带你 "看见" MCP 的过程,彻底理解 MCP 的概念
|
8天前
|
人工智能 Java 决策智能
Spring AI Alibaba Graph:多智能体框架实践
Spring AI Alibaba 是一个面向 Java 开发者的开源人工智能框架,旨在简化 AI 应用开发。本文重点介绍其 Graph 组件,用于解决工作流与多智能体协作问题。Graph 组件通过声明式编程接口,提供统一的上下文管理、消息记忆、人工确认节点等功能,支持复杂 AI 应用的构建。
|
8天前
|
人工智能 安全 测试技术
信条:阿里云AI攻防安全启示录
解读AI时代下的安全攻防新态势
1110 11
|
11天前
|
人工智能 IDE 开发工具