hibernate二级缓存是由SessionFactory管理,所以又叫SessionFactory级缓存,它是通过不同的类库来实现的,比如ehcache、oscache等。和一级缓存一样,二级缓存也是用来缓存实体对象的,对普通属性不缓存。
hibernate二级缓存的使用需要进行必要的配置,主要是四个地方(这里以ehcache为例):
1>. 配置echcache.xml文件
2>.开启二级缓存,修改hibernate.cfg.xml文件
<property name="hibernate.cache.use_second_level_cache">true</property>
3>.指定缓存产品提供商,修改hibernate.cfg.xml文件
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
4>.指定那些实体类使用二级缓存(两种方法)
1).在映射文件中采用<cache>标签
2).在hibernate.cfg.xml文件中,采用<class-cache>标签
<property name="hibernate.cache.use_second_level_cache">true</property>
3>.指定缓存产品提供商,修改hibernate.cfg.xml文件
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
4>.指定那些实体类使用二级缓存(两种方法)
1).在映射文件中采用<cache>标签
2).在hibernate.cfg.xml文件中,采用<class-cache>标签
hibernate二级缓存配置上之后,就成了“客观存在”,hibernate在使用某些方法的时候默认就使用和维护了二级缓存(哪怕你出于某种原因希望使用也不行)。因此,在使用二级缓存时进行一定的控制还是必要的,Session就提供了设置使用二级缓存的模式的方法(setCacheMode)来实现,当session调用某个方法时对二级缓存的存取改变。
1.实体类:
Student.java
public
class Student {
private Integer id;
private String name;
//一系列的setter.getter方法
}
private Integer id;
private String name;
//一系列的setter.getter方法
}
2.映射文件:
Student.hbm.xml
<
class
name
="com.sxt.hibernate.cache.entity.Student"
table
="sxt_hibernate_student"
>
<!-- 指定本类的对象使用二级缓存(这也可以放在hibernate.cfg.xml中统一指定) -->
<!--
<cache usage="read-only"/>
-->
< id name ="id" length ="4" >
< generator class ="native" > </ generator >
</ id >
< property name ="name" length ="10" > </ property >
</ class >
<!-- 指定本类的对象使用二级缓存(这也可以放在hibernate.cfg.xml中统一指定) -->
<!--
<cache usage="read-only"/>
-->
< id name ="id" length ="4" >
< generator class ="native" > </ generator >
</ id >
< property name ="name" length ="10" > </ property >
</ class >
3. 二级缓存配置文件:
ehcache.xml
<
ehcache
>
<!-- 当二级缓存溢出时,对象保存的临时磁盘路径 -->
< diskStore path ="java.io.tmpdir" />
<!-- name="sampleCache2" 缓存名字
maxElementsInMemory="1000" 缓存里可存放的最大对象数.
eternal="true" 缓存对象是否永久有效(true表示是).
timeToIdleSeconds="120" 对象在缓存中存活的空闲时间,即空闲多久它就失效,单位是秒.
timeToLiveSeconds="120" 对象在缓存中存活的时间,单位是秒.
overflowToDisk="true" 当缓存溢出时,对象是否保存到磁盘上.保存的磁盘路径由<diskStore>中的path指定.
-->
< defaultCache
maxElementsInMemory ="10000"
eternal ="false"
timeToIdleSeconds ="120"
timeToLiveSeconds ="120"
overflowToDisk ="true"
/>
</ ehcache >
<!-- 当二级缓存溢出时,对象保存的临时磁盘路径 -->
< diskStore path ="java.io.tmpdir" />
<!-- name="sampleCache2" 缓存名字
maxElementsInMemory="1000" 缓存里可存放的最大对象数.
eternal="true" 缓存对象是否永久有效(true表示是).
timeToIdleSeconds="120" 对象在缓存中存活的空闲时间,即空闲多久它就失效,单位是秒.
timeToLiveSeconds="120" 对象在缓存中存活的时间,单位是秒.
overflowToDisk="true" 当缓存溢出时,对象是否保存到磁盘上.保存的磁盘路径由<diskStore>中的path指定.
-->
< defaultCache
maxElementsInMemory ="10000"
eternal ="false"
timeToIdleSeconds ="120"
timeToLiveSeconds ="120"
overflowToDisk ="true"
/>
</ ehcache >
4.hibernate配置文件
hibernate.cfg.xml
<
hibernate-configuration
>
< session-factory >
< property name ="hibernate.connection.url" >jdbc:oracle:thin:@localhost:1521:ORCL10 </ property >
< property name ="hibernate.connection.driver_class" >oracle.jdbc.driver.OracleDriver </ property >
< property name ="hibernate.connection.username" >scott </ property >
< property name ="hibernate.connection.password" >yf123 </ property >
< property name ="hibernate.dialect" >org.hibernate.dialect.Oracle9Dialect </ property >
< property name ="hibernate.show_sql" >true </ property >
<!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 -->
< property name ="hibernate.cache.use_second_level_cache" >true </ property >
<!-- 指定二级缓存产品的提供商 -->
< property name ="hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider </ property >
< mapping resource ="com/sxt/hibernate/cache/entity/Student.hbm.xml" />
<!-- 指定那些类使用二级缓存 -->
< class-cache usage ="read-only" class ="com.sxt.hibernate.cache.entity.Student" />
</ session-factory >
</ hibernate-configuration >
< session-factory >
< property name ="hibernate.connection.url" >jdbc:oracle:thin:@localhost:1521:ORCL10 </ property >
< property name ="hibernate.connection.driver_class" >oracle.jdbc.driver.OracleDriver </ property >
< property name ="hibernate.connection.username" >scott </ property >
< property name ="hibernate.connection.password" >yf123 </ property >
< property name ="hibernate.dialect" >org.hibernate.dialect.Oracle9Dialect </ property >
< property name ="hibernate.show_sql" >true </ property >
<!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 -->
< property name ="hibernate.cache.use_second_level_cache" >true </ property >
<!-- 指定二级缓存产品的提供商 -->
< property name ="hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider </ property >
< mapping resource ="com/sxt/hibernate/cache/entity/Student.hbm.xml" />
<!-- 指定那些类使用二级缓存 -->
< class-cache usage ="read-only" class ="com.sxt.hibernate.cache.entity.Student" />
</ session-factory >
</ hibernate-configuration >
5.测试方法:
测试一:
public
static
void main(String[] args) {
Session session = null;
Transaction t = null;
/**
* 开启两个session中发出load查询
*/
/* try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 不会发出查询语句,因为开启了二级缓存,session共享二级缓存
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}*/
/**
* 开启两个session中发出get查询
*/
try {
session = HibernateUtils.getSession();
System.out.println(session);
t = session.beginTransaction();
Student student = (Student) session.get(Student. class, 1);
session.clear();
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 不会发出查询语句,因为开启了二级缓存,get使用二级缓存
Student student = (Student) session.get(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
Session session = null;
Transaction t = null;
/**
* 开启两个session中发出load查询
*/
/* try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 不会发出查询语句,因为开启了二级缓存,session共享二级缓存
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}*/
/**
* 开启两个session中发出get查询
*/
try {
session = HibernateUtils.getSession();
System.out.println(session);
t = session.beginTransaction();
Student student = (Student) session.get(Student. class, 1);
session.clear();
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 不会发出查询语句,因为开启了二级缓存,get使用二级缓存
Student student = (Student) session.get(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
测试二:
public
static
void main(String[] args) {
Session session = null;
Transaction t = null;
/**
* 开启两个session中发出load查询,使用SessionFactory清除二级缓存
*/
/* try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
//load时,会把对象放到两个缓存中
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
//用SessionFacotry管理二级缓存
SessionFactory factory=HibernateUtils.getSessionFactory();
//evict()把id为1的Student对象从二级缓存中清除.
factory.evict(Student.class, 1);
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 不会发出查询语句,因为开启了二级缓存,load也使用二级缓存
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}*/
/**
* 开启两个session中发出load查询,session对二级缓存的使用.
*/
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
//设置一级缓存和二级缓存的交互模式:
//GET表示在load时只是从二级缓存中读取数据,仅在数据更新时对二级缓存写数据。
//PUT表示只往二级缓存中放数据,而不从中取数据.
//NORMAL表示从二级缓存中读、写数据。
//REFRESH表示仅向二级缓存写数据,但不从二级缓存中读数据。
session.setCacheMode(CacheMode.GET);
Student student = (Student) session.load(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 会发出查询语句,因为前面设置了一级缓存和二级缓存的交互模式为GET,没有往二级缓存中放数据.
Student student = (Student) session.load(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
//设置交互模式为PUT.
session.setCacheMode(CacheMode.PUT);
// 会发出查询语句,因为设置了一级缓存和二级缓存的交互模式为PUT,只是往二级缓存中放数据,并不去中取数据.
Student student = (Student) session.load(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
Session session = null;
Transaction t = null;
/**
* 开启两个session中发出load查询,使用SessionFactory清除二级缓存
*/
/* try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
//load时,会把对象放到两个缓存中
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
//用SessionFacotry管理二级缓存
SessionFactory factory=HibernateUtils.getSessionFactory();
//evict()把id为1的Student对象从二级缓存中清除.
factory.evict(Student.class, 1);
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 不会发出查询语句,因为开启了二级缓存,load也使用二级缓存
Student student = (Student) session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}*/
/**
* 开启两个session中发出load查询,session对二级缓存的使用.
*/
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
//设置一级缓存和二级缓存的交互模式:
//GET表示在load时只是从二级缓存中读取数据,仅在数据更新时对二级缓存写数据。
//PUT表示只往二级缓存中放数据,而不从中取数据.
//NORMAL表示从二级缓存中读、写数据。
//REFRESH表示仅向二级缓存写数据,但不从二级缓存中读数据。
session.setCacheMode(CacheMode.GET);
Student student = (Student) session.load(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
// 会发出查询语句,因为前面设置了一级缓存和二级缓存的交互模式为GET,没有往二级缓存中放数据.
Student student = (Student) session.load(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
//设置交互模式为PUT.
session.setCacheMode(CacheMode.PUT);
// 会发出查询语句,因为设置了一级缓存和二级缓存的交互模式为PUT,只是往二级缓存中放数据,并不去中取数据.
Student student = (Student) session.load(Student. class, 1);
System.out.println( "student.name=" + student.getName());
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
本文转自NightWolves 51CTO博客,原文链接:http://blog.51cto.com/yangfei520/279239
,如需转载请自行联系原作者