激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Hibernate框架中的緩存技術(shù)詳解

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

2020-04-12 16:39TKD03072010 JAVA教程

這篇文章主要介紹了Hibernate框架中的緩存技術(shù),結(jié)合實例形式詳細(xì)分析了Hibernate框架緩存機制的原理與具體使用技巧,需要的朋友可以參考下

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

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

一級緩存:

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

舉個例子來說吧:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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();
// 出錯將回滾事務(wù)
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對象
HibernateUtil.closeSession(session);
}
}
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
第一次查詢:
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中緩存的實體對象

二級緩存:

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

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

1.加入緩存配置文件ehcache.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<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配置文件。

?
1
2
3
4
5
6
<!-- 開啟二級緩存 -->
<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)用到的實體對象 -->
<class-cache class="com.xqh.model.User" usage="read-only"></class-cache>

例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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();
// 出錯將回滾事務(wù)
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();
// 出錯將回滾事務(wù)
session.getTransaction().rollback();
} finally {
// 關(guān)閉Session對象
HibernateUtil.closeSession(session);
}
}
}

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

程序結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
第一次查詢:
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ù)應(yīng)用二級緩存,則性能方面會造成一定問題。

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

延伸 · 閱讀

精彩推薦
  • JAVA教程JAVA8 十大新特性詳解

    JAVA8 十大新特性詳解

    本教程將Java8的新特新逐一列出,并將使用簡單的代碼示例來指導(dǎo)你如何使用默認(rèn)接口方法,lambda表達(dá)式,方法引用以及多重Annotation,之后你將會學(xué)到最新...

    網(wǎng)絡(luò)4952019-06-17
  • JAVA教程java讀取文件顯示進度條的實現(xiàn)方法

    java讀取文件顯示進度條的實現(xiàn)方法

    當(dāng)讀取一個大文件時,一時半會兒無法看到讀取結(jié)果,就需要顯示一個進度條,是程序員明白已經(jīng)讀了多少文件,可以估算讀取還需要多少時間,下面的代碼...

    java教程網(wǎng)5092019-11-05
  • JAVA教程從Java的jar文件中讀取數(shù)據(jù)的方法

    從Java的jar文件中讀取數(shù)據(jù)的方法

    這篇文章主要介紹了從Java的jar文件中讀取數(shù)據(jù)的方法,實例分析了java檔案文件的相關(guān)操作技巧,需要的朋友可以參考下 ...

    liuzx321902019-12-20
  • JAVA教程淺析java貪心算法

    淺析java貪心算法

    這篇文章簡單主要介紹了java貪心算法,包含貪心算法的基本思路,性質(zhì),以及實現(xiàn)示例,有需要的小伙伴參考下 ...

    hebedich2472019-12-09
  • JAVA教程java實現(xiàn)單人版五子棋游戲

    java實現(xiàn)單人版五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)五子棋小游戲的相關(guān)資料,十分簡單實用,有不錯的參考借鑒價值,推薦給大家,需要的朋友可以參考下 ...

    lijiao1992020-03-31
  • JAVA教程命令行使用支持?jǐn)帱c續(xù)傳的java多線程下載器

    命令行使用支持?jǐn)帱c續(xù)傳的java多線程下載器

    java命令行下載器,支持?jǐn)帱c續(xù)傳下載,多線程下載,需要的朋友可以參考下 ...

    java教程網(wǎng)1792019-11-07
  • JAVA教程挑戰(zhàn)4道Java試題

    挑戰(zhàn)4道Java試題

    這篇文章主要為大家分享了4道Java基礎(chǔ)題,幫助大家鞏固基礎(chǔ)知識,夯實java基礎(chǔ)技能,感興趣的朋友快點挑戰(zhàn) ...

    _Himan_4652020-03-13
  • JAVA教程基于Spring框架的Shiro配置方法

    基于Spring框架的Shiro配置方法

    這篇文章主要介紹了基于Spring框架的Shiro配置方法,需要的朋友可以參考下 ...

    mdxy-dxy2662019-12-02
主站蜘蛛池模板: 99精品视频一区二区 | 性生活香蕉视频 | 欧美一区二区三区四区五区动图 | 3级毛片 | 二级大黄大片高清在线视频 | 国产一区二区精品在线观看 | 在线免费av网站 | 日韩精品中文字幕一区 | 久久欧美亚洲另类专区91大神 | 激情综合在线 | 毛片在线免费播放 | 精品一区久久久 | 日日爱99 | 国产一国产一级毛片视频 | av在线视 | 国产精品视频免费在线观看 | 一级免费黄色 | 成人毛片免费看 | 在线a毛片免费视频观看 | 国产中出视频 | 91avsese| 欧美a一| 欧美精品电影一区 | 色成人在线 | 91短视频在线播放 | 亚洲成人在线视频网 | 国产精品成人一区二区三区电影毛片 | 99精品视频网站 | 性色av一区二区三区四区 | 一级看片免费视频 | 免费看一级片 | 成人做爰高潮片免费视频韩国 | 草草视频在线 | 久久视频精品 | 女人叉开腿让男人桶 | 国产精品99久久久久久久女警 | 视频一区二区中文字幕 | 午夜影院操 | 91精品国产成人 | 黄色网址入口 | 国产亚洲精品久久久闺蜜 |