Hibernate框架中的緩存技術(shù)詳解
本文實(shí)例講述了Hibernate框架中的緩存技術(shù)。分享給大家供大家參考,具體如下:
Hibernate框架的緩存分為Session的緩存、SessionFactory的緩存,也稱為一級(jí)緩存和二級(jí)緩存。
一級(jí)緩存:
一級(jí)緩存是Session級(jí)的緩存,其生命周期很短,與Session相互對(duì)應(yīng),由Hibernate進(jìn)行管理,屬于事務(wù)范圍的緩存。當(dāng)程序調(diào)用 Session的load()方法、get()方法、save()方法、saveOrUpdate()方法、update()方法或查詢接口方法時(shí),Hibernate會(huì)對(duì)實(shí)體對(duì)象進(jìn)行緩存;當(dāng)通過load()方法或get()方法查詢實(shí)體對(duì)象時(shí),Hibernate會(huì)首先到緩存中查詢,在找不到實(shí)體對(duì)像的情況下,Hibernate才會(huì)發(fā)出SQL語(yǔ)句到數(shù)據(jù)庫(kù)中查詢,從而提高了Hibernate的使用效率。
舉個(gè)例子來說吧:
package com.xqh.util;
import org.hibernate.Session;
import com.xqh.model.User;
public class Test {
public static void main(String[] args) {
Session session = null;
try {
session = HibernateUtil.getSession(); // 獲取session
session.beginTransaction(); //開啟事務(wù)
System.out.println("第一次查詢:");
User user = (User)session.get(User.class, new Integer(1));
System.out.println("用戶名:" + user.getName());
System.out.println("第二次查詢:");
User user1 = (User)session.get(User.class, 1);
System.out.println("用戶名:" + user1.getName());
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// 出錯(cuò)將回滾事務(wù)
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對(duì)象
HibernateUtil.closeSession(session);
}
}
}
當(dāng)程序通過get()方法第一次查用戶對(duì)象時(shí),Hibernate會(huì)發(fā)出一條SQL語(yǔ)句進(jìn)行查詢,此時(shí)Hibernate對(duì)其用戶對(duì)象進(jìn)行了一級(jí)緩存;當(dāng)再次通過get()方法查詢時(shí),Hibernate就不會(huì)發(fā)出SQL語(yǔ)句了,因?yàn)橛脩裘呀?jīng)存在于一級(jí)緩存中。程序運(yùn)行結(jié)果:
第一次查詢: Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.sex as sex0_0_ from tb_user_info user0_ where user0_.id=? 用戶名:xqh 第二次查詢: 用戶名:xqh
注意:一級(jí)緩存的生命周期與Session相對(duì)應(yīng),它并不會(huì)在Session之間共享,在不同的Session中不能得到其他Session中緩存的實(shí)體對(duì)象
二級(jí)緩存:
二級(jí)緩存是SessionFactory級(jí)的緩存,其生命周期與SessionFactory一致。二級(jí)緩存可在多個(gè)Session間共享,屬于進(jìn)程范圍或群集范圍的緩存。
二級(jí)緩存是一個(gè)可插拔的緩存插件,它的使用需要第三方緩存產(chǎn)品的支持。在Hibernate框架中,通過Hibernate配置文件配置二級(jí)緩存的使用策略。
1.加入緩存配置文件ehcache.xml
<ehcache> <!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <diskStore path="java.io.tmpdir"/> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <!--Predefined caches. Add your cache configuration settings here. If you do not have a configuration for your cache a WARNING will be issued when the CacheManager starts The following attributes are required for defaultCache: name - Sets the name of the cache. This is used to identify the cache. It must be unique. maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <!-- Sample cache named sampleCache1 This cache contains a maximum in memory of 10000 elements, and will expire an element if it is idle for more than 5 minutes and lives for more than 10 minutes. If there are more than 10000 elements it will overflow to the disk cache, which in this configuration will go to wherever java.io.tmp is defined on your system. On a standard Linux system this will be /tmp" --> <cache name="sampleCache1" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <!-- Sample cache named sampleCache2 This cache contains 1000 elements. Elements will always be held in memory. They are not expired. --> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> --> <!-- Place configuration for your caches following --> </ehcache>
2.設(shè)置Hibernate配置文件。
<!-- 開啟二級(jí)緩存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 指定緩存產(chǎn)品提供商 --> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> <!-- 指定二級(jí)緩存應(yīng)用到的實(shí)體對(duì)象 --> <class-cache class="com.xqh.model.User" usage="read-only"></class-cache>
例:
package com.xqh.util;
import org.hibernate.Session;
import com.xqh.model.User;
public class Test {
public static void main(String[] args) {
Session session = null; // 第一個(gè)Session
try {
session = HibernateUtil.getSession();
session.beginTransaction();
System.out.println("第一次查詢:");
User user = (User)session.get(User.class, 1);
System.out.println("用戶名:" + user.getName());
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// 出錯(cuò)將回滾事務(wù)
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對(duì)象
HibernateUtil.closeSession(session);
}
try {
session = HibernateUtil.getSession(); // 開啟第二個(gè)緩存
session.beginTransaction();
System.out.println("第二次查詢:");
User user = (User)session.get(User.class, 1);
System.out.println("用戶名:" + user.getName());
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// 出錯(cuò)將回滾事務(wù)
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對(duì)象
HibernateUtil.closeSession(session);
}
}
}
二級(jí)緩存在Session之間是共享的,因此可在不同Session中加載同一個(gè)對(duì)象,Hibernate將只發(fā)出一條SQL語(yǔ)句,當(dāng)?shù)诙渭虞d對(duì)象時(shí),Hibernate將從緩存中獲取此對(duì)象。
程序結(jié)果:
第一次查詢: Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.sex as sex0_0_ from tb_user_info user0_ where user0_.id=? 用戶名:xqh 第二次查詢: 用戶名:xqh
對(duì)于二級(jí)緩存,可以使用一些不經(jīng)常更新的數(shù)據(jù)或參考的數(shù)據(jù),此時(shí)其性能會(huì)得到明顯的提升。但如果經(jīng)常變化的數(shù)據(jù)應(yīng)用二級(jí)緩存,則性能方面會(huì)造成一定問題。
希望本文所述對(duì)大家基于Hibernate框架的Java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java實(shí)現(xiàn)訂單超時(shí)自動(dòng)取消的7種方案
在電商、外賣、票務(wù)等系統(tǒng)中,訂單超時(shí)未支付自動(dòng)取消是一個(gè)常見的需求,這個(gè)功能乍一看很簡(jiǎn)單,甚至很多初學(xué)者會(huì)覺得:"不就是加個(gè)定時(shí)器么?" 但真到了實(shí)際工作中,細(xì)節(jié)的復(fù)雜程度往往會(huì)超乎預(yù)期,本文給大家介紹了Java實(shí)現(xiàn)訂單超時(shí)自動(dòng)取消的7種方案2024-12-12
Java hashCode原理以及與equals()區(qū)別聯(lián)系詳解
在 Java 應(yīng)用程序執(zhí)行期間,在同一對(duì)象上多次調(diào)用 hashCode 方法時(shí),必須一致地返回相同的整數(shù),前提是對(duì)象上 equals 比較中所用的信息沒有被修改。從某一應(yīng)用程序的一次執(zhí)行到同一應(yīng)用程序的另一次執(zhí)行,該整數(shù)無需保持一致2022-11-11
SpringBoot中使用HTTP客戶端工具Retrofit
這篇文章主要為大家介紹了SpringBoot中使用HTTP客戶端工具Retrofit方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

