Hibernate一级缓存

一级缓存生命周期很短,它session的生命周期一致,一级缓存也叫session级的缓存,也称事务级缓存

哪些方法支持一级缓存?
*load
*get
*iterate查询实体对象支持一级缓存,查询普通属性则不支持

一级缓存是缓存实体对象的

一级缓存如何管理?
* session.clear(),session.evict(对象名)

如何避免一次性打批量的实体数据插入后内存溢出
* 先flush,再clear


如果在现实工作中,遇到了大规模的数据,建议使用jdbc导入或数据库特定工具导入


/**
 * 测试一级缓存
 * @author Administrator
 *
 */
public class CacheLevel1Test extends TestCase {
	
	/**
	 * 发出两次load查询
	 */
	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());
			
			//不会发出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);
		}
	}

	/**
	 * 发出两次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());
			
			//不会发出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);
		}
	}

	/**
	 * 发出两次Iterate查询实体对象
	 */
	public void testCache3() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			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);
		}
	}

	/**
	 * 发出两次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);
		}
		
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			//会发出sql,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);
		}
		
	}

	/**
	 * 先执行save,再调用load加载save后的数据
	 */
	public void testCache6() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Student student = new Student();
			student.setName("李四");
			
			java.io.Serializable id = session.save(student);
			
			//不会发出sql,因为save是使用缓存的
			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);
		}
	}
	
	
	/**
	 * 缓存的管理,执行session.clear或session.evict方法,再调用load
	 */
	public void testCache7() {
		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.clear和session.evict
			//session.clear();
			session.evict(student);
			
			//会发出sql,因为一级缓存中的实体对象被清除了
			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);
		}
	}

	/**
	 * 向数据库中批量插入1000条数据
	 */
	public void testCache8() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			for (int i=0; i<1000; i++) {
				Student s = new Student();
				s.setName("s_" + i);
				session.save(s);
				if (i % 20 == 0) {
					session.flush();
					session.clear();
				}
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
}



你可能感兴趣的:(sql,Hibernate,工作,jdbc)