package myHibernate;
import java.io.Serializable;
import myHibernate.HibernateUtils;
import myHibernate.Student;
import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import junit.framework.TestCase;
public class CacheLevel2Test extends TestCase {
/**
* 开启个session,分别调用load
* 先将二级缓存设为false,也就是只使用session级的缓存,即不能跨session访问缓存,
* 所以会发出两次SQL语句;
* 然后将二级缓存设为true,可以debug观察发现只发出了一次SQL语句
*/
public void testCache1() {
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();
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);
}
}
//用get方法,也是一样支持缓存的,所以结果是一样的
public void testCache2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student 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
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student 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,分别调用load,第一个调用完之后便使用evict管理二级缓存,
* evict可以将某个类的所有缓存都清除,也可以只清除某一个对象,这时候要指明
* id
* */
public void testCache3() {
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);
}
SessionFactory sf = HibernateUtils.getSessionFactory();
//清除该类的所有对象
//sf.evict(Student.class);
//清除某个特定对象
sf.evict(Student.class, 1);
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与二级缓存的交互
* */
public void testCache4() {
Session session = null;
//第一个session
try {
session = HibernateUtils.getSession();
session.beginTransaction();
/*
* 设置CacheMode
* CacheMode.NORMAL :从二级缓存中读、写数据
* CacheMode.GET : 从二级缓存中读取数据,仅在数据更新时对二级缓存写数据
* CacheMode.PUT : 仅向二级缓存写数据,但不从二级缓存度数据
* */
System.out.println("第一个session");
session.setCacheMode(CacheMode.GET);
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);
}
SessionFactory sf = HibernateUtils.getSessionFactory();
//第二个session,不设置CacheMode,为默认状态,即会读、写缓存
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//会发出SQL语句,因为第一个session设置CacheMode.GET
System.out.println("第二个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,不设置CacheMode
try {
session = HibernateUtils.getSession();
session.beginTransaction();
System.out.println("第三个session");
//不会发错SQL语句,因为因为第二个session没有设置CacheMode,则缓存里有数据
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 ,将CacheMode设置为PUT,即只写不读
try {
session = HibernateUtils.getSession();
session.beginTransaction();
System.out.println("第四个session");
//会发出SQL语句,因为CacheMode设置为PUT,虽然缓存里有数据但不读取
session.setCacheMode(CacheMode.PUT);
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);
}
}
}
Hibernate二级缓存
最新推荐文章于 2024-01-31 01:25:56 发布