Hibernate一級緩存、二級緩存以及查詢緩存的關系


轉載自http://blog.csdn.net/maoyeqiu/article/details/50209893

 

前兩天總結了一下二級緩存和查詢緩存的關系,但是又有一個新的問題,就是查詢緩存緩存到二級緩存的數據,在第三次(第一次緩存中沒有數據,查詢數據庫將對應的ID值存入到二級緩存中去,第二次如果是同一個Session那么將會把數據一級緩存中的數據返回,如果不是同一個Session而是同一個sessionfactory,那么將會把二級緩存中的數據返回,同時將數據放入到一級緩存中去)獲取的時候,不使用查詢緩存的方法,而是直接使用一級緩存的方法,能不能將緩存的數據獲取到呢,我們現在驗證一下。

首先開啟一級緩存(默認)、二級緩存和查詢緩存

 

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  8.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>  
  9.         <property name="hibernate.connection.password">root</property>  
  10.         <property name="hibernate.connection.username">root</property>  
  11.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  12.         <property name="show_sql">true</property>  
  13.         <property name="hbm2ddl.auto">create</property>  
  14.         <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>  
  15.         <property name="hibernate.cache.use_query_cache">true</property>  
  16.         <!-- 開啟二級緩存,其實hibernate默認就是開啟的,這里顯示的指定一下 -->   
  17.                 <property name="hibernate.cache.use_second_level_cache">true</property>   
  18.         <property name="hibernate.generate_statistics">true</property>  
  19.         <mapping class="hibernate.test.dto.DepartmentEntity"></mapping>  
  20.     </session-factory>  
  21. </hibernate-configuration>  


用到的緩存類

 

 

[java]  view plain  copy
 
  1. package hibernate.test.dto;  
  2.   
  3. @Entity  
  4. @Table(name = "DEPARTMENT", uniqueConstraints = {  
  5.         @UniqueConstraint(columnNames = "ID"),  
  6.         @UniqueConstraint(columnNames = "NAME") })  
  7. @Cache(usage=CacheConcurrencyStrategy.READ_ONLY, region="department")  
  8. public class DepartmentEntity implements Serializable {  
  9.       
  10.     private static final long serialVersionUID = 1L;  
  11.   
  12.     @Id  
  13.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
  14.     @Column(name = "ID", unique = true, nullable = false)  
  15.     private Integer id;  
  16.       
  17.     @Column(name = "NAME", unique = true, nullable = false, length = 100)  
  18.     private String name;  
  19.   
  20.     public Integer getId() {  
  21.         return id;  
  22.     }  
  23.   
  24.     public void setId(Integer id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.   
  32.     public void setName(String name) {  
  33.         this.name = name;  
  34.     }  
  35. }  


啟動查詢緩存將返回的實體存入到二級緩存中去

 

 

[java]  view plain  copy
 
  1. //此方法向數據庫中存入三條數據  
  2. storeData();  
  3. Session session = HibernateUtil.getSessionFactory().openSession();  
  4. session.beginTransaction();  
  5. //開啟查詢緩存, query.setCacheable(true);開啟查詢緩存  
  6. Query query = session.createQuery("select s from DepartmentEntity s");     
  7. query.setCacheable(true);   
  8. List<DepartmentEntity> names = query.list();   
  9. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {   
  10.     String name = it.next().getName();   
  11.     System.out.println(name);   
  12. }   
  13. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  


執行的結果:

 

hibernate: select department0_.ID as ID0_, department0_.NAME as NAME0_ from DEPARTMENT department0_
Human Resource
Humanne
Jhon
3

我們可以看到,產生了一條查詢語句,將三個實體的名字輸出,放入二級緩存的數量是3,都是沒有問題的。這里比較容易犯的一個錯誤是查詢的結果並不是實體,而是名字或者是其他屬性,比如sql我們這么寫:select s.name from DepartmentEntity s。存入二級緩存的數據是0,也就是沒有存入到二級緩存中去。

再次調用上述方法:

 

[java]  view plain  copy
 
  1. //開啟查詢緩存, query.setCacheable(true);開啟查詢緩存  
  2. Query query = session.createQuery("select s from DepartmentEntity s");     
  3. query.setCacheable(true);   
  4. List<DepartmentEntity> names = query.list();   
  5. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {   
  6.     String name = it.next().getName();   
  7.     System.out.println(name);   
  8. }   
  9. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  10.   
  11. //同一個Session中再次查詢,不會發出SQL語句到數據庫  
  12. query = session.createQuery("select s from DepartmentEntity s");   
  13. query.setCacheable(true);   
  14. names = query.list();   
  15. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {   
  16.     String name = it.next().getName();   
  17.     System.out.println(name);   
  18. }   
  19. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  20. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());  



執行的結果:

 

Hibernate: select department0_.ID as ID0_, department0_.NAME as NAME0_ from DEPARTMENT department0_
Human Resource
Humanne
Jhon
3
Human Resource
Humanne
Jhon
3
0

我們可以看到只產生了一條數據庫查詢語句,第二次直接從緩存中獲取(注意,第二次調用的時候依然要寫quey.setCacheable(true); ,我們模擬的是同一個方法的多次調用,不然的會產生數據庫查詢),那么問題來了,這個緩存指的是一級緩存還是二級緩存呢,我們看執行的結果二級緩存的命中是0,也就是說這里並沒有用到二級緩存而是直接使用了一級緩存,前提是在同一個Session的情況下,在同一個session下執行的結果都會首先緩存到一級緩存中去,那么我們開一個新的Session會有什么樣的不同結果呢

 

[java]  view plain  copy
 
  1. //開啟查詢緩存, query.setCacheable(true);開啟查詢緩存  
  2. Query query = session.createQuery("select s from DepartmentEntity s");     
  3. query.setCacheable(true);   
  4. List<DepartmentEntity> names = query.list();   
  5. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {   
  6.     String name = it.next().getName();   
  7.     System.out.println(name);   
  8. }   
  9. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  10.   
  11. //同一個Session中再次查詢,不會發出SQL語句到數據庫  
  12. query = session.createQuery("select s from DepartmentEntity s");   
  13. quey.setCacheable(true);   
  14. names = query.list();   
  15. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {   
  16.     String name = it.next().getName();   
  17.     System.out.println(name);   
  18. }   
  19. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  20. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());  
  21.   
  22. Session anotherSession = HibernateUtil.getSessionFactory().openSession();  
  23. anotherSession.beginTransaction();  
  24. query = anotherSession.createQuery("select s from DepartmentEntity s");   
  25. query.setCacheable(true);   
  26. names = query.list();   
  27. for (Iterator<DepartmentEntity> it = names.iterator(); it.hasNext();) {   
  28.     String name = it.next().getName();   
  29.     System.out.println(name);   
  30. }   
  31. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  32. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());  


執行結果:

 

 

[java]  view plain  copy
 
  1. Hibernate: select department0_.ID as ID0_, department0_.NAME as NAME0_ from DEPARTMENT department0_  
  2. Human Resource  
  3. Humanne  
  4. Jhon  
  5. 3  
  6. Human Resource  
  7. Humanne  
  8. Jhon  
  9. 3  
  10. 0  
  11. Human Resource  
  12. Humanne  
  13. Jhon  
  14. 3  
  15. 3  


我們看到運行的結果,三次執行都只是放入二級緩存的實例3個,也就是說二級緩存中只有三個實例。由於二級緩存是sessionfactory級別的當開啟查詢緩存將數據放入的二級緩存的時候是不受開了幾個session影響的,所以盡管我們上邊開啟了2個session但是依舊是在二級緩存中有3個實體。這里還有一個問題是查詢緩存的key值是如何定義的呢,導致了開啟了3次查詢緩存而只存入3條數據,如果key值不同的話,那么肯定是會存入9條數據,關於這個問題大家可以參考,這里。由於二級緩存是sessionfactory級別的,因此會直接將二級緩存中的數據取出,並存入到一級緩存中去。

 

我們在sql中只是查詢實體的名字,我們來看一下查詢緩存是如何緩存的

 

[java]  view plain  copy
 
  1.               //開啟查詢緩存, query.setCacheable(true);開啟查詢緩存  
  2. ,Query query = session.createQuery("select s.name from DepartmentEntity s");     
  3. query.setCacheable(true);   
  4. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  5. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());  
  6.   
  7. //同一個Session中再次查詢,不會發出SQL語句到數據庫  
  8. query = session.createQuery("select s.name from DepartmentEntity s");   
  9. query.setCacheable(true);   
  10. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  11. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());  
  12.   
  13. Session anotherSession = HibernateUtil.getSessionFactory().openSession();  
  14. anotherSession.beginTransaction();  
  15. query = anotherSession.createQuery("select s.name from DepartmentEntity s");   
  16. query.setCacheable(true);   
  17. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCachePutCount());  
  18. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());  


執行結果:

 

Hibernate: select department0_.NAME as col_0_0_ from DEPARTMENT department0_
0
0
0
0
0
0

從結果上我們可以看到只有一個數據庫查詢,然后是六個0,前兩個0是沒有將數據存入到二級緩存中去,中間兩個0並且沒有sql數據庫查詢說明,數據從緩存中獲取,而且是一級緩存中的數據,后兩個0我們重新開啟了一個session,同樣沒有數據庫查詢,也就說是調用了二級緩存中的數據,也就說明了查詢緩存也是sessionfactory級別的。

我們現在來驗證一下我們剛才提出的問題,直接用load獲取數據

 

[java]  view plain  copy
 
  1. DepartmentEntity department = (DepartmentEntity) session.load(DepartmentEntity.class, new Integer(1));  
  2. System.out.println(HibernateUtil.getSessionFactory().getStatistics().getSecondLevelCacheHitCount());  

 

執行結果:

0

從結果來看,沒有產生數據庫查詢二級緩存的命中是0,也就是說數據是從一級緩存中獲取的,這就驗證了我們一開始提到的答案。

總結:

1、一級緩存是session級別的,二級緩存和查詢緩存都是sessionfactory級別的,查詢緩存和二級緩存是一起來使用的

2、任何sql執行都會存入到同一個session的一級緩存中去

3、同時開啟查詢緩存和二級緩存,可以在不同session間共享緩存的結果

4、二級緩存緩存的是實體,不是屬性

5、查詢緩存的結果如果只是屬性,那么查詢緩存中存儲的是id和屬性的值,如果是實體的集合,那么查詢緩存存儲的只是實體的id,對應的實體會存儲到二級緩存中去。

6、不同session間返回數據的順序是,二級緩存先將數據返回,然后將數據存入本session的一級緩存中去,以便下次調用時的使用


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM