首页 技术 正文
技术 2022年11月17日
0 收藏 327 点赞 4,188 浏览 4517 个字

hibernate 是一个持久层的框架,经常访问物理数据库,为了降低应用程序访问物理数据库的频次, 从而提升性能,

hibernate缓存机制分为: 一类是session 级缓存,二是sessionFactory级缓存

session是一级缓存  内置的,不能卸载的 是事务范围的缓存

sessionFactory 是二级缓存 SessionFactory对象的生命周期和应用程序的整个过程对应,因此Hibernate二级缓存是进程范围或者集群范围的缓存,有可能出现并发问题,因此需要采用适当的并发访问策略,该策略为被缓存的数据提供了事务隔离级别。第二级缓存是可选的,是一个可配置的插件,在默认情况下,SessionFactory不会启用这个插件。

二级缓存的配置:

(1)准备

把ehcache-1.2.3.jar加入到classpath中

在hibernate.cfg.xml中加入EHCache缓存插件的提供类

Category.hbm.xml<?xml version=”1.0″  encoding=”utf-8″?> <!DOCTYPE hibernate-mapping PUBLIC  “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd  “>
<hibernate-mapping>     <class  name=”org.qiujy.domain.cachedemo.Category” table=”categories”>        < !—              配置缓存,必须紧跟在class元素后面             对缓存中的Category对象采用读写型的并发访问策略         –>        <cache  usage=”read-write”/>               <id name=”id”  type=”java.lang.Long”>            <column name=”id” />            < generator class=”native” />        </id>        <!– 配置版本号,必须紧跟在id元素后面 –>        <version name=”version” column=”version”  type=”java.lang.Long” />               <property name=”name”  type=”java.lang.String”>            <column name=”name” length=”32″  not-null=”true”/>        </property>               < property name=”description” type=”java.lang.String”>            < column name=”description” length=”255″/>        < /property>               <set name=”products” table=”products”  cascade=”all” inverse=”true”>            <!– Hibernate只会缓存对象的简单属性的值,        要缓存集合属性,必须在集合元素中也加入<cache>子元素        而Hibernate仅仅是把与当前持久对象关联的对象的OID存放到缓存中。 如果希望把整个关联的对象的所有数据都存入缓存, 则要在相应关联的对象的映射文件中配置<cache>元素            –>            <cache usage=”read-write”/>                       <key column=”categoryId” not-null=”true”/>            < one-to-many class=”org.qiujy.domain.cachedemo.Product”/>        < /set>            < /class> </hibernate-mapping>Product.hbm.xml <?xml version=”1.0″  encoding=”utf-8″?> <!DOCTYPE hibernate-mapping PUBLIC  “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd  “>
<hibernate-mapping>     <class  name=”org.qiujy.domain.cachedemo.Product” table=”products”>               <cache usage=”read-write”/>               <id  name=”id” type=”java.lang.Long”>            <column name=”id”  />            <generator class=”native” />        < /id>        <!– 配置版本号,必须紧跟在id元素后面 –>        <version  name=”version” column=”version” type=”java.lang.Long” />               < property name=”name” type=”java.lang.String”>            <column  name=”name” length=”32″ not-null=”true”/>        < /property>               <property name=”description”  type=”java.lang.String”>            <column name=”description”  length=”255″/>        </property>               <property  name=”unitCost” type=”java.lang.Double”>            <column  name=”unitCost” />        </property>               < property name=”pubTime” type=”java.util.Date”>            <column  name=”pubTime” not-null=”true” />        </property>               <many-to-one name=”category”                  column=”categoryId”                 class=”org.qiujy.domain.cachedemo.Category”                 cascade=”save-update”                 not-null=”true”>         < /many-to-one>            < /class> </hibernate-mapping>2)      编辑ehcache.xml文件:  <ehcache>     <diskStore  path=”c://ehcache/”/>     <defaultCache          maxElementsInMemory=”10000″         eternal=”false”          timeToIdleSeconds=”120″         timeToLiveSeconds=”120″          overflowToDisk=”true”           />             <!– 设置Category类的缓存的数据过期策略 –>     <cache  name=”org.qiujy.domain.cachedemo.Category”          maxElementsInMemory=”100″         eternal=”true”          timeToIdleSeconds=”0″         timeToLiveSeconds=”0″          overflowToDisk=”false”         />              <!– 设置Category类的products集合的缓存的数据过期策略 –>      <cache  name=”org.qiujy.domain.cachedemo.Category.products”          maxElementsInMemory=”500″         eternal=”false”          timeToIdleSeconds=”300″         timeToLiveSeconds=”600″          overflowToDisk=”true”         />             <cache  name=”org.qiujy.domain.cachedemo.Product”          maxElementsInMemory=”500″         eternal=”false”          timeToIdleSeconds=”300″         timeToLiveSeconds=”600″          overflowToDisk=”true”         />     </ehcache>配置的元素说明: 元素或属性 描述 <diskStore> 设置缓存数据文件的存放目录 <defaultCache> 设置缓存的默认数据过期策略 <cache> 设定具体的命名缓存的数据过期策略 每个命名缓存代表一个缓存区域,每个缓存区域有各自的数据过期策略。命名缓存机制使得用户能够在每个类以及类的每个集合的粒度上设置数据过期策略。 cache元素的属性   name 设置缓存的名字,它的取值为类的全限定名或类的集合的名字 maxInMemory 设置基于内存的缓存中可存放的对象最大数目 eternal 设置对象是否为永久的,true表示永不过期,此时将忽略timeToIdleSeconds和timeToLiveSeconds属性; 默认值是false timeToIdleSeconds 设置对象空闲最长时间,超过这个时间,对象过期。当对象过期时,EHCache会把它从缓存中清除。 如果此值为0,表示对象可以无限期地处于空闲状态。 timeToLiveSeconds 设置对象生存最长时间,超过这个时间,对象过期。 如果此值为0,表示对象可以无限期地存在于缓存中。 overflowToDisk 设置基于内在的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862