亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

一級緩存:

一級緩存是Session級的緩存,其生命周期很短,與Session相互對應(yīng),由Hibernate進(jìn)行管理,屬于事務(wù)范圍的緩存。當(dāng)程序調(diào)用 Session的load()方法、get()方法、save()方法、saveOrUpdate()方法、update()方法或查詢接口方法時(shí),Hibernate會對實(shí)體對象進(jìn)行緩存;當(dāng)通過load()方法或get()方法查詢實(shí)體對象時(shí),Hibernate會首先到緩存中查詢,在找不到實(shí)體對像的情況下,Hibernate才會發(fā)出SQL語句到數(shù)據(jù)庫中查詢,從而提高了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對象
HibernateUtil.closeSession(session);
}
}
}

當(dāng)程序通過get()方法第一次查用戶對象時(shí),Hibernate會發(fā)出一條SQL語句進(jìn)行查詢,此時(shí)Hibernate對其用戶對象進(jìn)行了一級緩存;當(dāng)再次通過get()方法查詢時(shí),Hibernate就不會發(fā)出SQL語句了,因?yàn)橛脩裘呀?jīng)存在于一級緩存中。程序運(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

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

二級緩存:

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

二級緩存是一個(gè)可插拔的緩存插件,它的使用需要第三方緩存產(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.設(shè)置Hibernate配置文件。

<!-- 開啟二級緩存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定緩存產(chǎn)品提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 指定二級緩存應(yīng)用到的實(shí)體對象 -->
<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對象
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對象
HibernateUtil.closeSession(session);
}
}
}

二級緩存在Session之間是共享的,因此可在不同Session中加載同一個(gè)對象,Hibernate將只發(fā)出一條SQL語句,當(dāng)?shù)诙渭虞d對象時(shí),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ù),此時(shí)其性能會得到明顯的提升。但如果經(jīng)常變化的數(shù)據(jù)應(yīng)用二級緩存,則性能方面會造成一定問題。

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

相關(guān)文章

  • Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

    Spring Cloud OAuth2中/oauth/token的返回內(nèi)容格式

    Spring Cloud OAuth2 生成access token的請求/oauth/token的返回內(nèi)容就需要自定義,本文就詳細(xì)介紹一下,感興趣的可以了解一下
    2021-07-07
  • 一天時(shí)間用Java寫了個(gè)飛機(jī)大戰(zhàn)游戲,朋友直呼高手

    一天時(shí)間用Java寫了個(gè)飛機(jī)大戰(zhàn)游戲,朋友直呼高手

    前兩天我發(fā)現(xiàn)論壇有兩篇飛機(jī)大戰(zhàn)的文章異?;鸨?但都是python寫的,竟然不是我大Java,說實(shí)話作為老java選手,我心里是有那么一些失落的,難道我大java打飛機(jī)不行?今天特地整理了這篇文章,需要的朋友可以參考下
    2021-05-05
  • SpringBoot深入分析講解監(jiān)聽器模式下

    SpringBoot深入分析講解監(jiān)聽器模式下

    監(jiān)聽器模式,大家應(yīng)該并不陌生,主要的組成要素包括了事件、監(jiān)聽器以及廣播器;當(dāng)事件發(fā)生時(shí),廣播器負(fù)責(zé)將事件傳遞給所有已知的監(jiān)聽器,而監(jiān)聽器會對自己感興趣的事件進(jìn)行處理
    2022-07-07
  • SpringBoot整合ES高級查詢方式

    SpringBoot整合ES高級查詢方式

    這篇文章主要介紹了SpringBoot整合ES高級查詢方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 如何自定義Jackson序列化?@JsonSerialize

    如何自定義Jackson序列化?@JsonSerialize

    這篇文章主要介紹了如何自定義Jackson序列化?@JsonSerialize,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • MybatisPlus如何調(diào)用count函數(shù)

    MybatisPlus如何調(diào)用count函數(shù)

    這篇文章主要介紹了MybatisPlus如何調(diào)用count函數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • springboot注冊攔截器所遇到的問題

    springboot注冊攔截器所遇到的問題

    這篇文章主要介紹了springboot注冊攔截器的方法及所遇到的問題,需要的朋友可以參考下
    2018-07-07
  • SpringBoot實(shí)現(xiàn)緩存預(yù)熱的幾種常用方案

    SpringBoot實(shí)現(xiàn)緩存預(yù)熱的幾種常用方案

    緩存預(yù)熱是指在 Spring Boot 項(xiàng)目啟動(dòng)時(shí),預(yù)先將數(shù)據(jù)加載到緩存系統(tǒng)(如 Redis)中的一種機(jī)制,本文給大家介紹了SpringBoot實(shí)現(xiàn)緩存預(yù)熱的幾種常用方案,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • 詳細(xì)分析Java內(nèi)部類——局部內(nèi)部類

    詳細(xì)分析Java內(nèi)部類——局部內(nèi)部類

    這篇文章主要介紹了Java局部內(nèi)部類的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java 內(nèi)部類的知識,感興趣的朋友可以了解下
    2020-08-08
  • Java之SpringBoot實(shí)現(xiàn)基本增刪改查(前后端分離版)

    Java之SpringBoot實(shí)現(xiàn)基本增刪改查(前后端分離版)

    這篇文章主要介紹了Java中SpringBoot如何實(shí)現(xiàn)基本的增刪改查,前后端分離版,沒有和前端進(jìn)行聯(lián)系,感興趣的小伙伴可以借鑒閱讀本文
    2023-03-03

最新評論