欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

Hibernate框架的緩存分為Session的緩存、SessionFactory的緩存,也稱(chēng)為一級(jí)緩存和二級(jí)緩存。

一級(jí)緩存:

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

舉個(gè)例子來(lái)說(shuō)吧:

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(); //開(kāi)啟事務(wù)
System.out.println("第一次查詢(xún):");
User user = (User)session.get(User.class, new Integer(1));
System.out.println("用戶(hù)名:" + user.getName());
System.out.println("第二次查詢(xún):");
User user1 = (User)session.get(User.class, 1);
System.out.println("用戶(hù)名:" + 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)程序通過(guò)get()方法第一次查用戶(hù)對(duì)象時(shí),Hibernate會(huì)發(fā)出一條SQL語(yǔ)句進(jìn)行查詢(xún),此時(shí)Hibernate對(duì)其用戶(hù)對(duì)象進(jìn)行了一級(jí)緩存;當(dāng)再次通過(guò)get()方法查詢(xún)時(shí),Hibernate就不會(huì)發(fā)出SQL語(yǔ)句了,因?yàn)橛脩?hù)名已經(jīng)存在于一級(jí)緩存中。程序運(yùn)行結(jié)果:

第一次查詢(xún):
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=?
用戶(hù)名:xqh
第二次查詢(xún):
用戶(hù)名: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框架中,通過(guò)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配置文件。

<!-- 開(kāi)啟二級(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("第一次查詢(xún):");
User user = (User)session.get(User.class, 1);
System.out.println("用戶(hù)名:" + 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(); // 開(kāi)啟第二個(gè)緩存
session.beginTransaction();
System.out.println("第二次查詢(xún):");
User user = (User)session.get(User.class, 1);
System.out.println("用戶(hù)名:" + 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é)果:

第一次查詢(xún):
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=?
用戶(hù)名:xqh
第二次查詢(xún):
用戶(hù)名:xqh

對(duì)于二級(jí)緩存,可以使用一些不經(jīng)常更新的數(shù)據(jù)或參考的數(shù)據(jù),此時(shí)其性能會(huì)得到明顯的提升。但如果經(jīng)常變化的數(shù)據(jù)應(yīng)用二級(jí)緩存,則性能方面會(huì)造成一定問(wèn)題。

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

相關(guān)文章

最新評(píng)論