最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Hibernate框架中的緩存技術(shù)詳解

 更新時間:2016年03月24日 09:46:56   作者:TKD03072010  
這篇文章主要介紹了Hibernate框架中的緩存技術(shù),結(jié)合實例形式詳細分析了Hibernate框架緩存機制的原理與具體使用技巧,需要的朋友可以參考下

本文實例講述了Hibernate框架中的緩存技術(shù)。分享給大家供大家參考,具體如下:

Hibernate框架的緩存分為Session的緩存、SessionFactory的緩存,也稱為一級緩存和二級緩存。

一級緩存:

一級緩存是Session級的緩存,其生命周期很短,與Session相互對應,由Hibernate進行管理,屬于事務范圍的緩存。當程序調(diào)用 Session的load()方法、get()方法、save()方法、saveOrUpdate()方法、update()方法或查詢接口方法時,Hibernate會對實體對象進行緩存;當通過load()方法或get()方法查詢實體對象時,Hibernate會首先到緩存中查詢,在找不到實體對像的情況下,Hibernate才會發(fā)出SQL語句到數(shù)據(jù)庫中查詢,從而提高了Hibernate的使用效率。

舉個例子來說吧:

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(); //開啟事務
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();
// 出錯將回滾事務
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對象
HibernateUtil.closeSession(session);
}
}
}

當程序通過get()方法第一次查用戶對象時,Hibernate會發(fā)出一條SQL語句進行查詢,此時Hibernate對其用戶對象進行了一級緩存;當再次通過get()方法查詢時,Hibernate就不會發(fā)出SQL語句了,因為用戶名已經(jīng)存在于一級緩存中。程序運行結(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

注意:一級緩存的生命周期與Session相對應,它并不會在Session之間共享,在不同的Session中不能得到其他Session中緩存的實體對象

二級緩存:

二級緩存是SessionFactory級的緩存,其生命周期與SessionFactory一致。二級緩存可在多個Session間共享,屬于進程范圍或群集范圍的緩存。

二級緩存是一個可插拔的緩存插件,它的使用需要第三方緩存產(chǎn)品的支持。在Hibernate框架中,通過Hibernate配置文件配置二級緩存的使用策略。

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.設置Hibernate配置文件。

<!-- 開啟二級緩存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定緩存產(chǎn)品提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 指定二級緩存應用到的實體對象 -->
<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; // 第一個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();
// 出錯將回滾事務
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對象
HibernateUtil.closeSession(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();
// 出錯將回滾事務
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對象
HibernateUtil.closeSession(session);
}
}
}

二級緩存在Session之間是共享的,因此可在不同Session中加載同一個對象,Hibernate將只發(fā)出一條SQL語句,當?shù)诙渭虞d對象時,Hibernate將從緩存中獲取此對象。

程序結(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īng)常更新的數(shù)據(jù)或參考的數(shù)據(jù),此時其性能會得到明顯的提升。但如果經(jīng)常變化的數(shù)據(jù)應用二級緩存,則性能方面會造成一定問題。

希望本文所述對大家基于Hibernate框架的Java程序設計有所幫助。

相關(guān)文章

  • Java實現(xiàn)訂單超時自動取消的7種方案

    Java實現(xiàn)訂單超時自動取消的7種方案

    在電商、外賣、票務等系統(tǒng)中,訂單超時未支付自動取消是一個常見的需求,這個功能乍一看很簡單,甚至很多初學者會覺得:"不就是加個定時器么?" 但真到了實際工作中,細節(jié)的復雜程度往往會超乎預期,本文給大家介紹了Java實現(xiàn)訂單超時自動取消的7種方案
    2024-12-12
  • ssm開發(fā)使用redis作為緩存的使用步驟

    ssm開發(fā)使用redis作為緩存的使用步驟

    在開發(fā)中經(jīng)常遇到大量的重復的,高并發(fā)的查詢,此時可以使用redis緩存。這篇文章主要介紹了ssm開發(fā)使用redis作為緩存的使用步驟,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 詳解java迭代器模式

    詳解java迭代器模式

    這篇文章主要介紹了java迭代器模式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • spring注解@Service注解的使用解析

    spring注解@Service注解的使用解析

    這篇文章主要介紹了spring注解@Service注解的使用解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 基于Java中的數(shù)值和集合詳解

    基于Java中的數(shù)值和集合詳解

    下面小編就為大家?guī)硪黄贘ava中的數(shù)值和集合詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Java hashCode原理以及與equals()區(qū)別聯(lián)系詳解

    Java hashCode原理以及與equals()區(qū)別聯(lián)系詳解

    在 Java 應用程序執(zhí)行期間,在同一對象上多次調(diào)用 hashCode 方法時,必須一致地返回相同的整數(shù),前提是對象上 equals 比較中所用的信息沒有被修改。從某一應用程序的一次執(zhí)行到同一應用程序的另一次執(zhí)行,該整數(shù)無需保持一致
    2022-11-11
  • RocketMQ延遲消息簡明介紹

    RocketMQ延遲消息簡明介紹

    這篇文章主要介紹了RocketMQ延遲消息,延遲消息是個啥?顧名思義,就是等一段時間再消費的消息。文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • SpringBoot中使用HTTP客戶端工具Retrofit

    SpringBoot中使用HTTP客戶端工具Retrofit

    這篇文章主要為大家介紹了SpringBoot中使用HTTP客戶端工具Retrofit方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • SpringBoo項目打war包的多種方式

    SpringBoo項目打war包的多種方式

    在idea中,經(jīng)常會對web項目進行打jar包或者war包,jar包在Java環(huán)境中運行,war包在Tomcat服務器中跑,對于打war包有多種方式,一下介紹3鐘方式,需要的朋友可以參考下
    2024-06-06
  • 深入理解Java中的HashMap

    深入理解Java中的HashMap

    HashMap是Java程序員使用頻率最高的用于映射(鍵值對)處理的數(shù)據(jù)類型。隨著JDK(Java Developmet Kit)版本的更新,JDK1.8對HashMap底層的實現(xiàn)進行了優(yōu)化,例如引入紅黑樹的數(shù)據(jù)結(jié)構(gòu)和擴容的優(yōu)化等。本文將深入探討HashMap的結(jié)構(gòu)實現(xiàn)和功能原理
    2021-06-06

最新評論

福清市| 太仓市| 内丘县| 赤壁市| 贺兰县| 安陆市| 黔南| 平江县| 壤塘县| 礼泉县| 佛教| 大同县| 丰台区| 封丘县| 阜宁县| 海丰县| 阳春市| 和龙市| 龙井市| 衡阳县| 栾川县| 芜湖县| 光泽县| 琼中| 郴州市| 宁国市| 定陶县| 扎囊县| 乐安县| 屏东市| 确山县| 英吉沙县| 水富县| 文登市| 洛川县| 宝应县| 化德县| 上饶县| 鱼台县| 桂阳县| 潞西市|