一般來講ORM中的緩存分爲以下幾類:
1.事務級緩存:即在當前事務範圍內的數據緩存.就Hibernate來講,事務級緩存是基於Session的生命週期實現的,每個Session內部會存在一個數據緩存,它隨着Session的創建而存在,隨着Session的銷毀而滅亡,因此也稱爲Session Level Cache.
2.應用級緩存:即在某個應用中或應用中某個獨立數據庫訪問子集中的共享緩存,此緩存可由多個事務共享(數據庫事務或應用事務),事務之間的緩存共享策略與應用的事務隔離機制密切相關.在Hibernate中,應用級緩存由SessionFactory實現,所有由一個SessionFactory創建的Session實例共享此緩存,因此也稱爲SessionFactory Level Cache.
3.分佈式緩存:即在多個應用實例,多個JVM間共享的緩存策略.分佈式緩存由多個應用級緩存實例組成,通過某種遠程機制(RMI,JMS)實現各個緩存實例間的數據同步,任何一個實例的數據修改,將導致整個集羣間的數據狀態同步.
Hibernate數據緩存:
1.內部緩存(Session Level Cache也稱一級緩存):
舉例說明:
java 代碼
public class Test {
public void get(){
Session session = HibernateSessionFactory.getSession();
TUser t = (TUser)session.get("hibernate.TUser", 2);
System.out.println(t.getName());
session.close();
}
}
進行測試:在控制檯打印出一條SQL語句:Hibernate: select tuser0_.id as id0_0_, tuser0_.name as name0_0_, tuser0_.sex as sex0_0_ from test.t_user tuser0_ where tuser0_.id=? 說明進行了一次數據庫的調用.
代碼更改如下:
public class Test {
public void get(){
Session session = HibernateSessionFactory.getSession();
TUser t = (TUser)session.get("hibernate.TUser", 2);
System.out.println(t.getName());
TUser tt = (TUser)session.get("hibernate.TUser", 2);
System.out.println(tt.getName());
session.close();
}
}
再進行測試:進行了兩次查詢,控制檯仍然只打出一條SQL語句:Hibernate: select tuser0_.id as id0_0_, tuser0_.name as name0_0_, tuser0_.sex as sex0_0_ from test.t_user tuser0_ where tuser0_.id=? 說明還是隻進行了一次數據庫的調用.
再將代碼更改如下:
public class Test {
public void get(){
Session session = HibernateSessionFactory.getSession();
TUser t = (TUser)session.get("hibernate.TUser", 2);
System.out.println(t.getName());
session.close();
Session session1 = HibernateSessionFactory.getSession();
TUser tt = (TUser)session1.get("hibernate.TUser", 2);
System.out.println(tt.getName());
session1.close();
}
}
繼續測試:進行兩次查詢控制檯打印兩條SQL語句:Hibernate: select tuser0_.id as id0_0_, tuser0_.name as name0_0_, tuser0_.sex as sex0_0_ from test.t_user tuser0_ where tuser0_.id=?
Hibernate: select tuser0_.id as id0_0_, tuser0_.name as name0_0_, tuser0_.sex as sex0_0_ from test.t_user tuser0_ where tuser0_.id=?
結論:Hibernate進行查詢時總是先在緩存中進行查詢,如緩存中沒有所需數據才進行數據庫的查詢.Hibernate的內部緩存是基於Session的生命週期的,也就是說存在於每個Session內部,它隨着Session的創建而存在,隨着Session的銷毀而滅亡,內部緩存一般由Hibernate自動維護,不需要人爲幹預,當然我們也可以根據需要進行相應操作:Session.evict(Object)(將指定對象從內部緩存清除),Session.clear()(清空內部緩存).(如在兩次查詢間加入Session.clear()將會清空內部緩存,使得一個Sesion內部的兩次相同的查詢要對數據庫進行兩次操作).
2.二級緩存:(有時稱爲SessionFactory Level Cache)
Hibernate本身並未提供二級緩存的產品化實現(只提供了一個基於HashTable的簡單緩存以供調試),這裏我使用的是第三方緩存組件:EHcache.Hibernate的二級緩存實現需要進行以下配置(Hibernate3):
首先在hibernate.cfg.xml內添加:
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_query_cache">true</property>
然後在映射文件中添加:
<cache usage="read-only"/>
測試上面代碼:控制檯輸出多了這樣一句[ WARN] (CacheFactory.java:43) - read-only cache configured for mutable class: hibernate.TUser,二級緩存啓用成功!!
java 代碼
public class Test {
public void executeQuery(){
List list = new ArrayList();
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery("from TUser t");
query.setCacheable(true);//激活查詢緩存
list = query.list();
session.close();
}
public void get(){
Session session = HibernateSessionFactory.getSession();
TUser t = (TUser)session.get("hibernate.TUser", 2);
System.out.println(t.getName());
session.close();
}
}
測試:控制檯只輸出一條SQL語句:Hibernate: select tuser0_.id as id0_, tuser0_.name as name0_, tuser0_.sex as sex0_ from test.t_user tuser0_(即Query query = session.createQuery("from TUser t")這句代碼所對應的SQL). executeQuery()方法與get()方法使用的是不同的Session!!可是executeQuery()方法與get()方法只對數據庫進行了一次操作,這就是二級緩存在起作用了.
結論:Hibernate二級緩存是SessionFactory級的緩存,它允許多個Session間共享,使用時需要使用第三方的緩存組件,新版Hibernate將EHcache作爲默認的二級緩存實現.
緩存同步策略:緩存同步策略決定了數據對象在緩存中的存取規則,我們必須爲每個實體類指定相應的緩存同步策略.Hibernate中提供了4種不同的緩存同步策略:(暫時只記個概念吧)
1.read-only:只讀.對於不會發生改變的數據可使用(對數據只能查詢,其他的增刪改都會報錯不關是1或2緩存中).
2.nonstrict-read-write:如果程序對併發訪問下的數據同步要求不嚴格,且數據更新頻率較低,採用本緩存同步策略可獲得較好性能.(不能在二級緩存進行增刪改都會報錯)
3.read-write:嚴格的讀寫緩存.基於時間戳判定機制,實現了"read committed"事務隔離等級.用於對數據同步要求的情況,但不支持分佈式緩存,實際應用中使用最多的緩存同步策略.(都可以比較常用的)
4.transactional:事務型緩存,必須運行在JTA事務環境中.此緩存中,緩存的相關操作被添加到事務中(此緩存類似於一個內存數據庫),如事務失敗,則緩衝池的數據會一同回滾到事務的開始之前的狀態.事務型緩存實現了"Repeatable read"事務隔離等級,有效保證了數據的合法性,適應於對關鍵數據的緩存,Hibernate內置緩存中,只有JBossCache支持事務型緩存.
create table teamEH (id varchar(32),teamname varchar(32));
create table studentEH (id varchar(32),name varchar(32),team_id varchar(32));
POJO:
package EHCache;
public class Student ...{
private String id; //標識id
private String name; //學生姓名
private Team team;//班級
public String getName() ...{
return name;
}
public void setId(String id) ...{
this.id = id;
}
public void setName(String stuName) ...{
this.name = stuName;
}
public String getId() ...{
return id;
}
public Student() ...{ //無參的構造函數
}
public Team getTeam() ...{
return team;
}
public void setTeam(Team team) ...{
this.team = team;
}
}
package EHCache;
import java.util.HashSet;
import java.util.Set;
public class Team ...{
private String id;
private Set students;
private String teamName;
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public String getTeamName() ...{
return teamName;
}
public void setTeamName(String name) ...{
this.teamName = name;
}
public Set getStudents() ...{
return students;
}
public void setStudents(Set students) ...{
this.students = students;
}
}
Team.hbm.xml
其中<cache>標籤表示對student集合緩存,但只緩存id,如果需要緩存student實例,則需要在student.hbm.xml中的
class標籤中配置<cache>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
<class name="EHCache.Team" table="teamEH" lazy="false">
<id name="id" column="id">
<generator class="uuid.hex"></generator>
</id>
<property name="teamName" column="teamName"></property>
<set name="students"
lazy="true"
inverse="true"
outer-join="false"
batch-size="2"
cascade="save-update"
>
<!-- 對students集合緩存,但只是緩存student-id如果要對整個對象緩存,
還需要在Student.hbm.xml的class標籤中加入<cache>標籤 -->
<cache usage="read-write"/>
<key column="team_id"></key>
<one-to-many class="EHCache.Student"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
<class name="EHCache.Student" table="studentEH" lazy="false">
<cache usage="read-write"/>
<id name="id" column="id" unsaved-value="null">
<generator class="uuid.hex"></generator>
</id>
<property name="name" column="name"></property>
<many-to-one name="team"
column="team_id"
outer-join="true"
cascade="save-update"
class="EHCache.Team"></many-to-one>
</class>
</hibernate-mapping>
Hibernate.cfg.xml
配置hibernate.cache.provider_class以啓用EHCache
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312&useUnicode=true
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<mapping resource="EHCache/Student.hbm.xml" />
<mapping resource="EHCache/Team.hbm.xml" />
</session-factory>
</hibernate-configuration>
EHCache.xml(放在classpath下)
<ehcache>
<diskStore path="c:/cache"/> <!--緩存文件存放位置-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cache name="EHCache.Student"
maxElementsInMemory="500" <!---超過500實例,就將多出的部分放置緩存文件中->
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>
測試代碼(插入準備數據部分)
package EHCache;
import java.io.File;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/EHCache"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
// Team team=new Team();
// team.setTeamName("team1");
//
//
// for(int i=0;i<1000;i++){
// Student stu=new Student();
// stu.setName("tom"+i);
// stu.setTeam(team);
// session.save(stu);
// }
// tx.commit();
//
}
}
測試成功後,運行以下代碼
package EHCache;
import java.io.File;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/EHCache"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
//模擬多用戶訪問數據
Session session1=sessionFactory.openSession();
Transaction tx1=session1.beginTransaction();
List list=session1.createQuery("from Student").list();
for(int i=0;i<list.size();i++)...{
Student stu=(Student)list.get(i);
System.out.println(stu.getName());
}
tx1.commit();
session1.close();
Session session2=sessionFactory.openSession();
Transaction tx2=session2.beginTransaction();
//這個uuid從剛纔插入的數據中複製一個student的id
Student stu=(Student)session2.get(Student.class, "4028818316d184820116d184900e0001");
System.out.println(stu.getName());
tx2.commit();
session2.close();
}
}
結果如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select student0_.id as id0_, student0_.name as name0_, student0_.team_id as team3_0_ from studentEH student0_
Hibernate: select team0_.id as id1_0_, team0_.teamName as teamName1_0_ from teamEH team0_ where team0_.id=?
tom0
tom1
tom2
tom3
tom4
tom5
tom6
tom7
tom8
tom9
tom10
........................................
tom974
tom975
tom976
tom977
tom978
tom998
tom999
Hibernate: select team0_.id as id1_0_, team0_.teamName as teamName1_0_ from teamEH team0_ where team0_.id=?
tom0
可以看到,第二次查詢,已經不再訪問數據庫了,而且,查看c:/cache文件夾,也可以看到,數據已經緩存成功了