心如止水_00 发表于 2013-03-07 17:41

hibernate的二级缓存介绍

 hibernate二级缓存是由SessionFactory管理,所以又叫SessionFactory级缓存,它是通过不同的类库来实现的,比如ehcache、oscache等。和一级缓存一样,二级缓存也是用来缓存实体对象的,对普通属性不缓存。

  hibernate二级缓存的使用需要进行必要的配置,主要是四个地方(这里以ehcache为例):

  1>. 配置echcache.xml文件 放在src下

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

  true

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

  org.hibernate.cache.EhCacheProvider

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

  1).在映射文件中采用标签

  2).在hibernate.cfg.xml文件中,采用标签

  hibernate二级缓存配置上之后,就成了“客观存在”,hibernate在使用某些方法的时候默认就使用和维护了二级缓存(哪怕你出于某种原因希望使用也不行)。因此,在使用二级缓存时进行一定的控制还是必要的,Session就提供了设置使用二级缓存的模式的方法 (setCacheMode)来实现,当session调用某个方法时对二级缓存的存取改变。

  1.实体类:


 Student.java
  public class Student {
  private Integer id;
  private String name;
  //一系列的setter.getter方法
  }

心如止水_00 发表于 2013-03-07 17:42

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>

心如止水_00 发表于 2013-03-07 17:43

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>

心如止水_00 发表于 2013-03-07 17:44

http://www.233.com/Java/jichu/20100304/084634850.html
页: [1]
查看完整版本: hibernate的二级缓存介绍