对于Hibernate这类ORM而言,缓存显的尤为重要,它是持久层性能提升的关键.简单来讲Hibernate就是对JDBC进行封装,以实现内部状态的管理,OR关系的映射等,但随之带来的就是数据访问效率的降低,和性能的下降,而缓存就是弥补这一缺点的重要方法.
缓存就是数据库数据在内存中的临时容器,包括数据库数据在内存中的临时拷贝,它位于数据库与数据库访问层中间.ORM在查询数据时首先会根据自身的缓存管理策略,在缓存中查找相关数据,如发现所需的数据,则直接将此数据作为结果加以利用,从而避免了数据库调用性能的开销.而相对内存操作而言,数据库调用是一个代价高昂的过程.
一般来讲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文件夹,也可以看到,数据已经缓存成功了