hibernate一级缓存
一级缓存很短和session的生命周期一致,一级缓存也叫session级的缓存或事务级缓存
那些方法支持一级缓存:
* get()
* load()
* iterate(查询实体对象)
如何管理一级缓存:
* session.clear(),session.evict()
如何避免一次性大量的实体数据入库导致内存溢出
* 先flush,再clear
如果数据量特别大,考虑采用jdbc实现,如果jdbc也不能满足要求可以考虑采用数据本身的特定导入工具
什么时候用一级缓存?
不是经常改变的,相对静止的
eternal="false"不是 永恒的
expires有效期
ExportDB:
create table t_classes (id integer not null auto_increment, name varchar(255), primary key (id))
create table t_student (id integer not null auto_increment, name varchar(255), classesid integer, primary key (id))
alter table t_student add index FK4B9075708EBC77F7 (classesid), add constraint FK4B9075708EBC77F7 foreign key (classesid) references t_classes (id)
mysql> create database hibernate_cache;
Query OK, 1 row affected (0.11 sec)
mysql> use hibernate_cache;
Database changed
InitData:
Hibernate: insert into t_classes (name) values (?)
Hibernate: insert into t_student (name, classesid) values (?, ?)
package com.bjsxt.hibernate;
import java.io.Serializable;
import org.hibernate.Session;
import junit.framework.TestCase;
public class CacheLevel1Test extends TestCase {
/**
* 在同一个session中发出两次load查询
*/
public void testCache1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//不会发出sql,因为load支持lazy,
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
//不会发出sql,因为load使用缓存
student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次get查询
*/
public void testCache2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//马上会发出sql,因为get不支持lazy,
Student student = (Student)session.get(Student.class, 1);
System.out.println("student.name=" + student.getName());
//不会发出sql,因为get使用缓存
student = (Student)session.get(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次iterate查询实体对象
*/
public void testCache3() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//会发出N——1条查询sql(此处为二条)
Student student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + student.getName());
//会发出查询id的sql,不会发出查询实体对象的sql,因为iterate使用缓存
student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次iterate查询普通属性对象
*/
public void testCache4() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
String name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + name);
//iterate查询普通属性,一级缓存不会缓存,所以发出sql
//一级缓存只是缓存实体对象的
name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + name);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 开启两个session中发出load查询
*/
public void testCache5() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
//第二个session中
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//会发出查询语句,session间不能共享一级缓存的数据
//因为它会伴随session的生命周期存在和消亡
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中先save,再发出load查询save过的数据
*/
public void testCache6() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student stu = new Student();
stu.setName("王五");
Serializable id = session.save(stu);
//不会发出查询sql,因为save是使用缓存的(在数据库中有相应的记录,与一级缓存session有关,纳入它的管理)
Student student = (Student)session.load(Student.class, id);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 向数据库中批量加入1000条数据
*/
public void testCache7() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
for (int i=0; i<1000; i++) {
Student student = new Student();
student.setName("s_" + i);
session.save(student);
//每20条数据就强制session将数据持久化
//同时清除缓存,避免大量数据造成内存溢出
if ( i % 20 == 0) {
session.flush();
session.clear();
}
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中依次load clear load
*/
public void testCache8() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//不会马上发出sql,因为load支持lazy,
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
//我们可以用clear或者evict管理一级缓存
session.clear();//清除缓存中的数据
//会发出sql,因为load使用缓存
student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
一级缓存很短和session的生命周期一致,一级缓存也叫session级的缓存或事务级缓存
那些方法支持一级缓存:
* get()
* load()
* iterate(查询实体对象)
如何管理一级缓存:
* session.clear(),session.evict()
如何避免一次性大量的实体数据入库导致内存溢出
* 先flush,再clear
如果数据量特别大,考虑采用jdbc实现,如果jdbc也不能满足要求可以考虑采用数据本身的特定导入工具
什么时候用一级缓存?
不是经常改变的,相对静止的
eternal="false"不是 永恒的
expires有效期
ExportDB:
create table t_classes (id integer not null auto_increment, name varchar(255), primary key (id))
create table t_student (id integer not null auto_increment, name varchar(255), classesid integer, primary key (id))
alter table t_student add index FK4B9075708EBC77F7 (classesid), add constraint FK4B9075708EBC77F7 foreign key (classesid) references t_classes (id)
mysql> create database hibernate_cache;
Query OK, 1 row affected (0.11 sec)
mysql> use hibernate_cache;
Database changed
InitData:
Hibernate: insert into t_classes (name) values (?)
Hibernate: insert into t_student (name, classesid) values (?, ?)
package com.bjsxt.hibernate;
import java.io.Serializable;
import org.hibernate.Session;
import junit.framework.TestCase;
public class CacheLevel1Test extends TestCase {
/**
* 在同一个session中发出两次load查询
*/
public void testCache1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//不会发出sql,因为load支持lazy,
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
//不会发出sql,因为load使用缓存
student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次get查询
*/
public void testCache2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//马上会发出sql,因为get不支持lazy,
Student student = (Student)session.get(Student.class, 1);
System.out.println("student.name=" + student.getName());
//不会发出sql,因为get使用缓存
student = (Student)session.get(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次iterate查询实体对象
*/
public void testCache3() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//会发出N——1条查询sql(此处为二条)
Student student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + student.getName());
//会发出查询id的sql,不会发出查询实体对象的sql,因为iterate使用缓存
student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次iterate查询普通属性对象
*/
public void testCache4() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
String name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + name);
//iterate查询普通属性,一级缓存不会缓存,所以发出sql
//一级缓存只是缓存实体对象的
name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + name);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 开启两个session中发出load查询
*/
public void testCache5() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
//第二个session中
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//会发出查询语句,session间不能共享一级缓存的数据
//因为它会伴随session的生命周期存在和消亡
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中先save,再发出load查询save过的数据
*/
public void testCache6() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student stu = new Student();
stu.setName("王五");
Serializable id = session.save(stu);
//不会发出查询sql,因为save是使用缓存的(在数据库中有相应的记录,与一级缓存session有关,纳入它的管理)
Student student = (Student)session.load(Student.class, id);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 向数据库中批量加入1000条数据
*/
public void testCache7() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
for (int i=0; i<1000; i++) {
Student student = new Student();
student.setName("s_" + i);
session.save(student);
//每20条数据就强制session将数据持久化
//同时清除缓存,避免大量数据造成内存溢出
if ( i % 20 == 0) {
session.flush();
session.clear();
}
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中依次load clear load
*/
public void testCache8() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//不会马上发出sql,因为load支持lazy,
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
//我们可以用clear或者evict管理一级缓存
session.clear();//清除缓存中的数据
//会发出sql,因为load使用缓存
student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}