Hibernate 缓存机制详细讲解

 

hibernate查询缓存

查询缓存是针对普通属性结果集的缓存
对实体对象的结果集只缓存id

查询缓存的生命周期,当前关联的表发生修改,那么查询缓存生命周期结束

查询缓存的配置和使用:
 * 在hibernate.cfg.xml文件中启用查询缓存,如:
 <property name="hibernate.cache.use_query_cache">true</property>
 * 在程序中必须手动启用查询缓存,如:
 query.setCacheable(true);


 ===

ehcache.xml:

<!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> -->

    <!-- Place configuration for your caches following -->

</ehcache>
====

hibernate.cfg.xml:

<!-- 开启二级缓存 -->
  <property name="hibernate.cache.use_second_level_cache">true</property>
  
  <!-- 指定缓存产品提供商 -->
  <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
  
  <!-- 启用查询缓存 -->
  <property name="hibernate.cache.use_query_cache">true</property>
  
  <mapping resource="com/bjsxt/hibernate/Classes.hbm.xml"/>
  <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
  
  <class-cache class="com.bjsxt.hibernate.Student" usage="read-only"/>
 </session-factory>
</hibernate-configuration>

 

===

/**
  * 开启查询缓存,关闭二级缓存
  * 
  * 开启一个session,分别调用query.list
  */

Query query = session.createQuery("select s.name from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   List names = query.list(); 
   for (Iterator iter=names.iterator();iter.hasNext(); ) {
    String name = (String)iter.next();
    System.out.println(name);
   }
   
   System.out.println("-------------------------------------");
   query = session.createQuery("select s.name from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   //没有发出查询sql,因为启用了查询缓存
   names = query.list(); 
   for (Iterator iter=names.iterator();iter.hasNext(); ) {
    String name = (String)iter.next();
    System.out.println(name);
   }

   session.getTransaction().commit();

 

----

Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s.name from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   List names = query.list(); 
   for (Iterator iter=names.iterator();iter.hasNext(); ) {
    String name = (String)iter.next();
    System.out.println(name);
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  System.out.println("-------------------------------------");
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s.name from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   //不会发出查询sql,因为查询缓存的生命周期和session无关
   List names = query.list(); 
   for (Iterator iter=names.iterator();iter.hasNext(); ) {
    String name = (String)iter.next();
    System.out.println(name);
   }
   session.getTransaction().commit();

----

/**
  * 开启查询缓存,关闭二级缓存
  * 
  * 开启两个session,分别调用query.iterate
  */

Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s.name from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
    
   for (Iterator iter=query.iterate();iter.hasNext(); ) {
    String name = (String)iter.next();
    System.out.println(name);
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  System.out.println("-------------------------------------");
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s.name from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
    
   //查询缓存只对query.list()起作用,query.iterate不起作用,也就是query.iterate不使用
   //查询缓存
   for (Iterator iter=query.iterate();iter.hasNext(); ) {
    String name = (String)iter.next();
    System.out.println(name);
   }
   session.getTransaction().commit();

----

/**
  * 关闭查询缓存,关闭二级缓存
  * 
  * 开启两个session,分别调用query.list查询实体对象
  */

Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s from Student s");
   //启用查询查询缓存
   //query.setCacheable(true);
   
   List students = query.list(); 
   for (Iterator iter=students.iterator();iter.hasNext(); ) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  System.out.println("-------------------------------------");
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s from Student s");
   //启用查询查询缓存
   //query.setCacheable(true);
   //会发出查询sql,因为list默认每次都会发出查询sql
   List students = query.list(); 
   for (Iterator iter=students.iterator();iter.hasNext(); ) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();

---

/**
  * 开启查询缓存,关闭二级缓存
  * 
  * 开启两个session,分别调用query.list查询实体对象
  */

Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   List students = query.list(); 
   for (Iterator iter=students.iterator();iter.hasNext(); ) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  System.out.println("-------------------------------------");
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   //会发出n条查询语句,因为开启了查询缓存,关闭了二级缓存,那么查询缓存会缓存实体对象的id
   //所以hibernate会根据实体对象的id去查询相应的实体,如果缓存中不存在相应的
   //实体那么将发出根据实体id查询的sql语句,否则不会发出sql使用缓存中的数据
   List students = query.list(); 
   for (Iterator iter=students.iterator();iter.hasNext(); ) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();

---


 /**
  * 开启查询缓存,开启二级缓存
  * 
  * 开启两个session,分别调用query.list查询实体对象
  */

Session session = null;
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   List students = query.list(); 
   for (Iterator iter=students.iterator();iter.hasNext(); ) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();
  }catch(Exception e) {
   e.printStackTrace();
   session.getTransaction().rollback();
  }finally {
   HibernateUtils.closeSession(session);
  }
  
  System.out.println("-------------------------------------");
  
  try {
   session = HibernateUtils.getSession();
   session.beginTransaction();
   
   Query query = session.createQuery("select s from Student s");
   //启用查询查询缓存
   query.setCacheable(true);
   
   //不会发出查询sql,因为开启了二级缓存和查询缓存,查询缓存缓存了实体对象的id列表
   //hibernate会根据实体对象的id列表到二级缓存中取得相应的数据
   List students = query.list(); 
   for (Iterator iter=students.iterator();iter.hasNext(); ) {
    Student student = (Student)iter.next();
    System.out.println(student.getName());
   }
   session.getTransaction().commit();

posted @ 2017-02-22 15:13  JAVA小白鼠  阅读(251)  评论(0编辑  收藏  举报