Hibernate中提供了两级缓存,一级缓存是Session级别的缓存,它属于事务范围的缓存,该级缓存由hibernate管理,应用程序无需干预;二级缓存是SessionFactory级别的缓存,该级缓存可以进行配置和更改,并且可以动态加载和卸载,hibernate还为查询结果提供了一个查询缓存,它依赖于二级缓存;
一,缓存的概念
缓存是位于应用程序和永久性数据存储源之间用于临时存放复制数据的内存区域,缓存可以降低应用程序之间读写永久性数据存储源的次数,从而提高应用程序的运行性能;
hibernate在查询数据时,首先会到缓存中查找,如果找到就直接使用,找不到时才从永久性数据存储源中检索,因此,把频繁使用的数据加载到缓存中,可以减少应用程序对永久性数据存储源的访问,使应用程序的运行性能得以提升;
二,缓存的范围
缓存范围决定了缓存的生命周期,缓存范围分为3类:
1>事务范围
缓存只能被当前事务访问,缓存的生命周期依赖于事务的生命周期,事务结束时,缓存的生命周期也结束了;
2>进程范围
缓存被进程内的所有事务共享,这些事务会并发访问缓存,需要对缓存采用必要的事务隔离机制,缓存的生命周期取决与进程的生命周期,进程结束,缓存的生命周期也结束了;
3>集群范围
缓存被一个或多个计算机的进程共享,缓存中的数据被复制到集群中的每个进行节点,进程间通过远程通信来保证缓存中数据的一致性;
在查询时,如果在事务范围内的缓存中没有找到,可以到进程范围或集群范围的缓存中查找,如果还没找到,则到数据库中查询;
三,Hibernate中的第一级缓存
Hibernate的一级缓存由Session提供,只存在于Session的生命周期中,当应用程序调用Session接口的save(),update(),saveOrupDate(),get(),load()或者Query和Criteria实例的list(),iterate()等方法时,如果Session缓存中没有相应的对象,hibernate就会把对象加入到一级缓存中,当session关闭时,该Session所管理的一级缓存也会立即被清除;
1,get查询测试:
1>在同一个session中发出两次get查询
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess = HibernateSessionFactory.getSession();
-
Transaction tx = sess.beginTransaction();
-
Student s1 = (Student)sess.get(Student.class, 2);
-
System.out.println(s1.getName());
-
Student s2 = (Student)sess.get(Student.class, 2);
-
System.out.println(s2.getName());
-
tx.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
2>开启两个session中发出两次get查询
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess1 = HibernateSessionFactory.getSession();
-
Transaction tx1 = sess1.beginTransaction();
-
Student s1 = (Student)sess1.get(Student.class, 2);
-
System.out.println(s1.getName());
-
tx1.commit();
-
HibernateSessionFactory.closeSession();
-
Session sess2 = HibernateSessionFactory.getSession();
-
Transaction tx2 = sess2.beginTransaction();
-
Student s2 = (Student)sess2.get(Student.class, 2);
-
System.out.println(s2.getName());
-
tx2.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
2,iterate查询测试
插入一个iteritor查询方式:
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess = HibernateSessionFactory.getSession();
-
Transaction tx = sess.beginTransaction();
-
Query query = sess.createQuery( "from Student");
-
Iterator iter = query.iterate();
-
while(iter.hasNext()){
-
System.out.println(((Student)iter.next()).getName());
-
}
-
tx.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess = HibernateSessionFactory.getSession();
-
Transaction tx = sess.beginTransaction();
-
Student s1 = (Student)sess.createQuery( "from Student s where s.id = 2").iterate().next();
-
System.out.println(s1.getName());
-
Student s2 = (Student)sess.createQuery( "from Student s where s.id = 2").iterate().next();
-
System.out.println(s2.getName());
-
tx.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
3,iterate查询属性测试:
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess = HibernateSessionFactory.getSession();
-
Transaction tx = sess.beginTransaction();
-
String name1 = (String)sess.createQuery( "select s.name from Student s where s.id = 2").iterate().next();
-
System.out.println(name1);
-
String name2 = (String)sess.createQuery( "select s.name from Student s where s.id = 2").iterate().next();
-
System.out.println(name2);
-
tx.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
4,在一个session中先save,再执行load查询
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess = HibernateSessionFactory.getSession();
-
Transaction tx = sess.beginTransaction();
-
Student s = new Student( 8, "newAcc", 8, 8);
-
Serializable id = sess.save(s);
-
tx.commit();
-
Student s1 = (Student)sess.load(Student.class, 8);
-
System.out.println(s1.getName());
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
--------------------
Session接口为应用程序提供了两个管理缓存的方法:
1>evict()方法:用于将某个对象从Session的一级缓存中清除;
2>clear()方法:用于将一级缓存中的所有对象全部清楚;
测试:
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess = HibernateSessionFactory.getSession();
-
Transaction tx = sess.beginTransaction();
-
Student s = (Student)sess.load(Student.class, 1);
-
System.out.println(s.getName());
-
sess.clear(); //清除一级缓存中的所有对象
-
Student s1 = (Student)sess.load(Student.class, 1);
-
System.out.println(s1.getName());
-
tx.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
四,Hibernate中的第二级缓存
二级缓存是一个可插拔的缓存插件,它是由SessionFactory负责管理的;
由于SessionFactory对象的生命周期与应用程序的整个过程对应,通常一个应用程序对应一个SessionFactory,因此,二级缓存是进程范围或者集群范围的缓存;
与一级缓存一样,二级缓存也是根据对象的id来加载与缓存,当执行某个查询获得结果集为实体对象集时,hibernate就会把它们按照对象id加载到二级缓存中,在访问指定的id的对象时,首先从一级缓存中查找,找到就直接使用,找不到则转到二级缓存中查找(必须配置且启用二级缓存),如果二级缓存中找到,则直接使用,否则会查询数据库,并将查询结果根据对象的id放到缓存中;
1,常用的二级缓存插件
Hibernate的二级缓存功能是通过配置二级缓存插件来实现的,常用的二级缓存插件包括EHCache,OSCache,SwarmCache和JBossCache。其中EHCache缓存插件是理想的进程范围的缓存实现,此处以使用EHCache缓存插件为例,来介绍如何使用hibernate的二级缓存;
2,Hibernate中使用EHCache的配置
1>引入EHCache相关的jar包;
lib\optional\ehcache下的三个jar包;
2>创建EHCache的配置文件ehcache.xml
-
<span style="font-size:18px;"> <strong> <ehcache>
-
<diskStore path="java.io.tmpdir"/>
-
<defaultCache
-
maxElementsInMemory= "10000"
-
eternal= "false"
-
timeToIdleSeconds= "120"
-
timeToLiveSeconds= "120"
-
overflowToDisk= "true"
-
/>
-
<cache name="sampleCache1"
-
maxElementsInMemory= "10000"
-
eternal= "false"
-
timeToIdleSeconds= "300"
-
timeToLiveSeconds= "600"
-
overflowToDisk= "true"
-
/>
-
<cache name="sampleCache2"
-
maxElementsInMemory= "1000"
-
eternal= "true"
-
timeToIdleSeconds= "0"
-
timeToLiveSeconds= "0"
-
overflowToDisk= "false"
-
/>
-
</ehcache> </strong> </span>
在defaultCache元素中,maxElementsInMemory属性设置缓存对象的最大数目;eternal属性指定是否永不过期,true为不过期,false为过期;timeToldleSeconds属性设置对象处于空闲状态的最大秒数;timeToLiveSeconds属性设置对象处于缓存状态的最大秒数;overflowToDisk属性设置内存溢出时是否将溢出对象写入硬盘;
3>在Hibernate配置文件里面启用EHCache
在hibernate.cfg.xml配置文件中,启用EHCache的配置如下:
-
<span style="font-size:18px;"> <strong> <!-- 启用二级缓存 -->
-
<property name="hibernate.cache.use_second_level_cache">true </property>
-
<!-- 设置二级缓存插件EHCache的Provider类 -->
-
<property name="hibernate.cache.region.factory_class">
-
org.hibernate.cache.ehcache.EhCacheRegionFactory </property> </strong> </span>
1>>在实体类的映射文件里面配置
在需要进行缓存的持久化对象的映射文件中配置相应的二级缓存策略,如User,hbm.xml:
-
<span style="font-size:18px;"> <strong> xml version="1.0" encoding="UTF-8"
-
-
-
-
<hibernate-mapping>
-
<class name="com.hibtest1.entity.User" table="user" catalog="bookshop">
-
<cache usage="read-write"/>
-
<id name="id" type="java.lang.Integer">
-
<column name="Id" />
-
<generator class="native" />
-
</id>
-
<property name="loginName" type="java.lang.String">
-
<column name="LoginName" length="50" />
-
</property>
-
-
</class>
-
</hibernate-mapping> </strong> </span>
注意:<cache>元素只能放在<class>元素的内部,而且必须处在<id>元素的前面,<cache>元素放在哪些<class>元素下面,就说明会对哪些类进行缓存;
2>>在hibernate配置文件中统一配置,强烈推荐使用这种方式:
在hibernate.cfg.xml文件中使用<class-cache>元素来配置哪些实体类的对象需要二级缓存:
<span style="font-size:18px;"><strong><span style="font-size:18px;"><strong><span style="font-size:18px;"><strong><class-cache usage="read-only" class="com.anlw.entity.Student"/></strong></span></strong></span></strong></span>
在<class-cache>元素中,usage属性指定缓存策略,需要注意<class-cache>元素必须放在所有<mapping>元素的后面;3,Hibernate中使用EHCache的测试:
-
<span style= "font-size:18px;"><strong> public void Query(){
-
Session sess1 = HibernateSessionFactory.getSession();
-
Student s1 = (Student)sess1.get(Student.class, 1);
-
System.out.println(s1.getName());
-
HibernateSessionFactory.closeSession();
-
Session sess2 = HibernateSessionFactory.getSession();
-
Student s2 = (Student)sess2.get(Student.class, 1);
-
System.out.println(s2.getName());
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
注意:在hibernate配置二级缓存时属性的顺序如下,顺序错了会空指针异常:
-
<span style= "font-size:18px;"><strong> <!-- 启用二级缓存 -->
-
<property name= "hibernate.cache.use_second_level_cache"> true</property>
-
<!-- 设置二级缓存插件EHCache的Provider类 -->
-
<property name= "hibernate.cache.region.factory_class">
-
org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
-
<mapping class= "com.anlw.entity.Student"/>
-
< class-cache usage= "read-only" class= "com.anlw.entity.Student"/></strong></span>
五,Hibernate中的查询缓存
对于经常使用的查询语句,如果启用了查询缓存 ,当第一次执行查询语句时,hibernate会将查询结果存储在二级缓存中,以后再次执行该查询语句时,从缓存中获取查询结果,从而提高查询性能;
hibernate的查询缓存主要是针对普通属性结果集的缓存,而对于实体对象的结果集只缓存id;
查询缓存的生命周期,若当前关联的表发生修改,那么查询缓存的生命周期结束;
1,查询缓存的配置
查询缓存基于二级缓存,使用查询缓存前,必须首先配置好二级缓存;
在配置了二级缓存的基础上,在hibernate的配置文件hibernate.cfg.xml中添加如下配置,可以启用查询缓存:
<property name="hibernate.cache.use_query_cache">false</property>
此外在程序中还必须手动启用查询缓存:
query.setCacheable(true);
2,测试查询缓存:
1>开启查询缓存,关闭二级缓存,开启一个session,分别调用query.list查询属性,测试前,先在先在hibernate.cfg.xml文件中开启查询缓存,关闭二级缓存,如下所示:
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
-
<span style= "font-size:18px;"><strong> public static void query(){
-
Session sess = HibernateSessionFactory.getSession();
-
Transaction tx = sess.beginTransaction();
-
Query query = sess.createQuery( "select s.name from Student s");
-
query.setCacheable( true);
-
List names = query.list();
-
for(Iterator iter = names.iterator();iter.hasNext();){
-
String name = (String)iter.next();
-
System.out.println(name);
-
}
-
System.out.println( "----------");
-
query = sess.createQuery( "select s.name from Student s");
-
query.setCacheable( true);
-
names = query.list();
-
for(Iterator iter = names.iterator();iter.hasNext();){
-
String name = (String)iter.next();
-
System.out.println(name);
-
}
-
tx.commit();
-
HibernateSessionFactory.closeSession();
-
}
-
</strong></span>
第二次没有去查数据库,因为启用了查询缓存;
2>开启查询缓存,关闭二级缓存,开启两个session,分别调用query.list查询属性,测试前,先在先在hibernate.cfg.xml文件中开启查询缓存,关闭二级缓存,如下所示:
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
-
<span style= "font-size:18px;"><strong> public static void query(){
-
Session sess1 = HibernateSessionFactory.getSession();
-
Transaction tx1 = sess1.beginTransaction();
-
Query query = sess1.createQuery( "select s.name from Student s");
-
query.setCacheable( true);
-
List names = query.list();
-
for(Iterator iter = names.iterator();iter.hasNext();){
-
String name = (String)iter.next();
-
System.out.println(name);
-
}
-
tx1.commit();
-
HibernateSessionFactory.closeSession();
-
System.out.println( "----------");
-
Session sess2 = HibernateSessionFactory.getSession();
-
Transaction tx2 = sess2.beginTransaction();
-
query = sess2.createQuery( "select s.name from Student s");
-
query.setCacheable( true);
-
names = query.list();
-
for(Iterator iter = names.iterator();iter.hasNext();){
-
String name = (String)iter.next();
-
System.out.println(name);
-
}
-
tx2.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
3>开启查询缓存,关闭二级缓存,开启两个session,分别调用query.list查询实体对象,测试前,先在先在hibernate.cfg.xml文件中开启查询缓存,关闭二级缓存,如下所示:
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
-
<span style= "font-size:18px;"><strong> public static void query(){
-
Session sess1 = HibernateSessionFactory.getSession();
-
Transaction tx1 = sess1.beginTransaction();
-
Query query = sess1.createQuery( "from Student");
-
query.setCacheable( true);
-
List student = query.list();
-
for(Iterator iter = student.iterator();iter.hasNext();){
-
Student s = (Student)iter.next();
-
System.out.println(s.getName()+ "--"+s.getAge());
-
}
-
tx1.commit();
-
HibernateSessionFactory.closeSession();
-
System.out.println( "----------");
-
Session sess2 = HibernateSessionFactory.getSession();
-
Transaction tx2 = sess2.beginTransaction();
-
query = sess2.createQuery( "from Student");
-
query.setCacheable( true);
-
student = query.list();
-
for(Iterator iter = student.iterator();iter.hasNext();){
-
Student s = (Student)iter.next();
-
System.out.println(s.getName()+ "--"+s.getAge());
-
}
-
tx2.commit();
-
HibernateSessionFactory.closeSession();
-
}</strong></span>
-
<span style= "font-size:18px;"><strong>Hibernate:
-
select
-
student0_.id as id1_0_,
-
student0_.age as age2_0_,
-
student0_.name as name3_0_
-
from
-
student student0_
-
anliwenaaa-- 1
-
test-- 2
-
----------
-
Hibernate:
-
select
-
student0_.id as id1_0_0_,
-
student0_.age as age2_0_0_,
-
student0_.name as name3_0_0_
-
from
-
student student0_
-
where
-
student0_.id=?
-
Hibernate:
-
select
-
student0_.id as id1_0_0_,
-
student0_.age as age2_0_0_,
-
student0_.name as name3_0_0_
-
from
-
student student0_
-
where
-
student0_.id=?
-
anliwenaaa-- 1
-
test-- 2</strong></span>
4>开启查询缓存,开启二级缓存,开启两个session,分别调用query.list查询实体对象,测试前,先在先在hibernate.cfg.xml文件中开启查询缓存,开启二级缓存,如下所示:
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
代码和3>一样,但是结果不同,第二次不会发出sql,因为开启了二级缓存和缓存查询,查询缓存缓存了实体对象的id,hibernate会根据实体对象的id到二级缓存中取得相应的数据;
转载至https://blog.csdn.net/an_2016/article/details/52088712
1921

被折叠的 条评论
为什么被折叠?



