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共享二級緩存
}*/
* 開啟兩個session中發出get查詢
try {
System.out.println(session);
Student student = (Student) session.get(Student.class, 1);
session.clear();
System.out.println("student.name=" + student.getName());
} catch (Exception e) {
} finally {
// 不會發出查詢語句,因為開啟了二級緩存,get使用二級緩存
}
測試二:
* 開啟兩個session中發出load查詢,使用SessionFactory清除二級緩存
//load時,會把對象放到兩個緩存中
//用SessionFacotry管理二級緩存
SessionFactory factory=HibernateUtils.getSessionFactory();
//evict()把id為1的Student對象從二級緩存中清除.
factory.evict(Student.class, 1);
// 不會發出查詢語句,因為開啟了二級緩存,load也使用二級緩存
* 開啟兩個session中發出load查詢,session對二級緩存的使用.
//設定一級緩存和二級緩存的互動模式:
//GET表示在load時隻是從二級緩存中讀取資料,僅在資料更新時對二級緩存寫資料。
//PUT表示隻往二級緩存中放資料,而不從中取資料.
//NORMAL表示從二級緩存中讀、寫資料。
//REFRESH表示僅向二級緩存寫資料,但不從二級緩存中讀資料。
session.setCacheMode(CacheMode.GET);
// 會發出查詢語句,因為前面設定了一級緩存和二級緩存的互動模式為GET,沒有往二級緩存中放資料.
//設定互動模式為PUT.
session.setCacheMode(CacheMode.PUT);
// 會發出查詢語句,因為設定了一級緩存和二級緩存的互動模式為PUT,隻是往二級緩存中放資料,并不去中取資料.
本文轉自NightWolves 51CTO部落格,原文連結:http://blog.51cto.com/yangfei520/279239,如需轉載請自行聯系原作者