hibernate缓存机制
来源:互联网 发布:三星s5230c软件 编辑:程序博客网 时间:2024/05/16 19:05
在hibernate的使用中,大家多数时间都在讨论一级缓存和二级缓存,而往往忽略了查询缓存。其实hibernate的查询缓存在使用过程中也起着同样重要的作用。hibernate的查询缓存是主要是针对普通属性结果集的缓存, 而对于实体对象的结果集只缓存id。在一级缓存,二级缓存和查询缓存都打开的情况下作查询操作时这样的:查询普通属性,会先到查询缓存中取,如果没有,则查询数据库;查询实体,会先到查询缓存中取id,如果有,则根据id到缓存(一级/二级)中取实体,如果缓存中取不到实体,再查询数据库。
和一级/二级缓存不同,查询缓存的生命周期 ,是不确定的,当前关联的表发生改变时,查询缓存的生命周期结束。
查询缓存的配置和使用也是很简单的:
1>查询缓存的启用不但要在配置文件中进行配置
<property name="hibernate.cache.use_query_cache">true</property>
2>还要在程序中显示的进行启用
query.setCacheable(true);
1>查询缓存的启用不但要在配置文件中进行配置
<property name="hibernate.cache.use_query_cache">true</property>
2>还要在程序中显示的进行启用
query.setCacheable(true);
1.实体类:
Student.jsva
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">
<generatorclass="native"></generator>
</id>
<propertyname="name"length="10"></property>
</class>
<!-- 指定本类的对象使用二级缓存(这也可以放在hibernate.cfg.xml中统一指定) -->
<!--
<cache usage="read-only"/>
-->
<id name="id" length="4">
<generatorclass="native"></generator>
</id>
<propertyname="name"length="10"></property>
</class>
3.hibernate配置文件:
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<propertyname="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL10</property>
<propertyname="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<propertyname="hibernate.connection.username">scott</property>
<propertyname="hibernate.connection.password">yf123</property>
<propertyname="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<propertyname="hibernate.show_sql">true</property>
<!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 -->
<propertyname="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定二级缓存产品的提供商 -->
<propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 启用查询缓存 -->
<propertyname="hibernate.cache.use_query_cache">true</property>
<mappingresource="com/sxt/hibernate/cache/entity/Student.hbm.xml"/>
<!-- 指定那些类使用二级缓存 -->
<class-cacheusage="read-only"class="com.sxt.hibernate.cache.entity.Student"/>
</session-factory>
</hibernate-configuration>
<session-factory>
<propertyname="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL10</property>
<propertyname="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<propertyname="hibernate.connection.username">scott</property>
<propertyname="hibernate.connection.password">yf123</property>
<propertyname="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<propertyname="hibernate.show_sql">true</property>
<!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 -->
<propertyname="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定二级缓存产品的提供商 -->
<propertyname="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 启用查询缓存 -->
<propertyname="hibernate.cache.use_query_cache">true</property>
<mappingresource="com/sxt/hibernate/cache/entity/Student.hbm.xml"/>
<!-- 指定那些类使用二级缓存 -->
<class-cacheusage="read-only"class="com.sxt.hibernate.cache.entity.Student"/>
</session-factory>
</hibernate-configuration>
4.测试方法:
public staticvoid main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启一个session,分别调用query.list
*/
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
/*
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
System.out.println("================================");
query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
//没有发出查询语句,因为这里使用的查询缓存
names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.list
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
//query.setCacheable(true);
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
//query.setCacheable(true);
//不会发出查询语句,因为查询缓存和session无关.
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.iterate
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
for (Iterator<String> it = query.iterate(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
//会发出查询语句,因为query.iterate不使用查询缓存
for (Iterator<String> it = query.iterate(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 关闭查询缓存,关闭二级缓存, 开启两个session,分别调用query.list查询实体对象
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
//query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
//query.setCacheable(true);
//会发出查询语句,因为list默认每次都会发出sql语句
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.list查询实体对象
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
//会发出根据id查询实体的n条查询语句,因为这种情况下,查询过程是这样的:
// 在第一次执行list时,会把查询对象的id缓存到查询缓存里
// 第二次执行list时, 会遍历查询缓存里的id到缓存里去找实体对象,由于这里没找到实体对象,
//所以就发出n条查询语句到数据库中查询.
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
@SuppressWarnings("unchecked")
public staticvoid main(String[] args) {
Session session = null;
Transaction t = null;
/**
* 开启查询缓存,开启二级缓存, 开启两个session,分别调用query.list查询实体对象
*/
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
//不会发出查询语句,因为这种情况下,查询过程是这样的:
// 在第一次执行list时,会把查询对象的id缓存到查询缓存里
// 第二次执行list时, 会遍历查询缓存里的id到缓存里去找实体对象,由于这里开启了二级缓存,可以找到目标实体对象,
//所以就不会再发出n条查询语句.
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启一个session,分别调用query.list
*/
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
/*
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
System.out.println("================================");
query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
//没有发出查询语句,因为这里使用的查询缓存
names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.list
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
//query.setCacheable(true);
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
//query.setCacheable(true);
//不会发出查询语句,因为查询缓存和session无关.
List<String> names = query.list();
for (Iterator<String> it = names.iterator(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.iterate
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
for (Iterator<String> it = query.iterate(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s.name from Student s");
//启用查询缓存
query.setCacheable(true);
//会发出查询语句,因为query.iterate不使用查询缓存
for (Iterator<String> it = query.iterate(); it.hasNext();) {
String name = it.next();
System.out.println(name);
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 关闭查询缓存,关闭二级缓存, 开启两个session,分别调用query.list查询实体对象
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
//query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
//query.setCacheable(true);
//会发出查询语句,因为list默认每次都会发出sql语句
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
/* @SuppressWarnings("unchecked")
public static void main(String[] args) {
Session session = null;
Transaction t = null;
*//**
* 开启查询缓存,关闭二级缓存, 开启两个session,分别调用query.list查询实体对象
*//*
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
//会发出根据id查询实体的n条查询语句,因为这种情况下,查询过程是这样的:
// 在第一次执行list时,会把查询对象的id缓存到查询缓存里
// 第二次执行list时, 会遍历查询缓存里的id到缓存里去找实体对象,由于这里没找到实体对象,
//所以就发出n条查询语句到数据库中查询.
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}*/
@SuppressWarnings("unchecked")
public staticvoid main(String[] args) {
Session session = null;
Transaction t = null;
/**
* 开启查询缓存,开启二级缓存, 开启两个session,分别调用query.list查询实体对象
*/
//如果不用查询缓存的话,那两个都发出查询语句,这也是默认的情况.
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
System.out.println("================================");
try {
session = HibernateUtils.getSession();
t = session.beginTransaction();
Query query = session.createQuery("select s from Student s");
//启用查询缓存
query.setCacheable(true);
//不会发出查询语句,因为这种情况下,查询过程是这样的:
// 在第一次执行list时,会把查询对象的id缓存到查询缓存里
// 第二次执行list时, 会遍历查询缓存里的id到缓存里去找实体对象,由于这里开启了二级缓存,可以找到目标实体对象,
//所以就不会再发出n条查询语句.
List<Student> students = query.list();
for (Iterator<Student> it = students.iterator(); it.hasNext();) {
Student s = it.next();
System.out.println(s.getName());
}
t.commit();
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
0 0
- Hibernate缓存机制介绍
- Hibernate:缓存机制解析
- Hibernate:缓存机制解析
- Hibernate的缓存机制
- Hibernate的缓存机制
- Hibernate的缓存机制
- HIbernate缓存机制
- Hibernate的缓存机制
- hibernate的缓存机制
- Hibernate缓存机制
- Hibernate缓存机制2
- Hibernate缓存机制介绍
- Hibernate缓存机制
- hibernate缓存机制
- hibernate 缓存机制
- hibernate缓存机制
- Hibernate缓存机制
- hibernate缓存机制
- exp\imp 导入导出命令使用
- linux命令行下载jdk
- stack的实现
- 口碑不错的国内外优秀稳定网络加速器收集
- 分布式日志收集系统Apache Flume的设计详细介绍
- hibernate缓存机制
- MySQL my.cnf参数配置优化详解
- VC/MFC之TreeCtrl控件使用经验总结
- 对话框包含EditText自动弹出软键盘
- 文本特征提取方法研究
- __bridge,__bridge_retained和__bridge_transfer的意思、区别与使用
- oracle表空间相关
- &和&&
- Linux下cut命令的使用