1. 程式人生 > >hibernate二級快取測試

hibernate二級快取測試

SessionFactory類:


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.stat.Statistics;

public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final
ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); private static Configuration configuration = new Configuration(); private static SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION; static { sessionFactory = configuration.configure(configFile).buildSessionFactory(); } private
HibernateSessionFactory(){ } public static Session getSession(){ Session session = threadLocal.get(); if(session == null || !session.isOpen()){ session = (sessionFactory != null) ? sessionFactory.openSession():null; threadLocal.set(session); } return
session; } public static Statistics getstat(){ return sessionFactory.getStatistics(); } public static void close(){ sessionFactory.close(); } }

在Stu.hbm.xml中加入(必須在配置id前完成)

<cache usage="read-write"/>

在hibernate.cfg.xml加入二級快取配置

<!-- 設定二級快取外掛EHCache的Provider類-->
        <property name="hibernate.cache.provider_class">
            net.sf.ehcache.hibernate.EhCacheProvider
        </property>
        <!-- 啟動"查詢快取" -->
        <property name="hibernate.cache.use_query_cache">
          true
        </property>

        <!-- 快取記憶體提供程式 -->  
        <property name="hibernate.cache.region.factory_class"> 
        org.hibernate.cache.ehcache.EhCacheRegionFactory 
      </property>

<!-- 開啟二級快取 --> 
        <property name="hibernate.cache.use_second_level_cache">true</property>
         <!-- 強制Hibernate以更人性化的格式將資料存入二級快取 -->  
        <property name="hibernate.cache.use_structured_entries">true</property> 

        <property name="current_session_context_class">thread</property>
        <property name="hibernate.generate_statistics">true</property>

Scenario1:
開啟查詢快取,開啟二級快取

import model.Stu;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.stat.Statistics;

public class CacheTest {

    public static void main(String[] args) {
        eacheTest();
    }

    public static void eacheTest(){
        Session s1 = HibernateSessionFactory.getSession();
        s1.beginTransaction();
        System.out.println("Search Stu first time.");
        Query q = s1.createQuery("from Stu");
        //啟用查詢快取
        //Enable/disable second level query (result) caching for this query.
        q.setCacheable(true);
        q.list();
//      q.iterate();

        System.out.println("put int level-two eache.");
        s1.getTransaction().commit();
        s1.close();

        System.out.println("-------------------------------------");

        Session s2 = HibernateSessionFactory.getSession();
        s2.beginTransaction();
        System.out.println("Search Stu second time, don't execute sql");
        Query q2 = s2.createQuery("from Stu");
        //啟用查詢快取
        q2.setCacheable(true);
        q2.list();
//      q2.iterate();
        s2.getTransaction().commit();
        s2.close();

        System.out.println("-------------------------------------");

        Session s3 = HibernateSessionFactory.getSession();
        Stu  stu = (Stu) s3.get(Stu.class, 1);
        Stu  stu2 = (Stu) s3.get(Stu.class, 1);
        Stu stu3 = (Stu)s3.get(Stu.class, 2);
        System.out.println(stu.getSname());
        System.out.println(stu2.getSname());
        System.out.println(stu3.getSname());
        s3.close();

        System.out.println("-------------------------------------");

        Statistics s = HibernateSessionFactory.getstat();
        s.logSummary();

        HibernateSessionFactory.close();
    }
}

執行結果:

Search Stu first time.
08-13 16:23:25.772 org.hibernate.cache.internal.StandardQueryCache DEBUG - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache
08-13 16:23:25.772 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2
08-13 16:23:25.772 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - Element for key sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2 is null
08-13 16:23:25.773 org.hibernate.cache.internal.StandardQueryCache DEBUG - Query results were not found in cache
Hibernate: 
    select
        stu0_.sno as sno1_2_,
        stu0_.sname as sname2_2_,
        stu0_.sex as sex3_2_,
        stu0_.age as age4_2_,
        stu0_.sdept as sdept5_2_ 
    from
        test.stu stu0_
08-13 16:23:25.841 org.hibernate.cache.internal.StandardQueryCache DEBUG - Caching query results in region: org.hibernate.cache.internal.StandardQueryCache; timestamp=5896004427124736
08-13 16:23:25.842 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2 value: [5896004427124736, 1, 2, 3, 4, 6, 7]
put int level-two eache.
08-13 16:23:25.851 org.hibernate.engine.internal.StatisticalLoggingSessionEventListener INFO  - Session Metrics {
    29941 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    17828019 nanoseconds spent preparing 1 JDBC statements;
    1520136 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    8472812 nanoseconds spent performing 7 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    568019 nanoseconds spent performing 1 L2C misses;
    6330335 nanoseconds spent executing 1 flushes (flushing a total of 6 entities and 0 collections);
    62448 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
-------------------------------------
Search Stu second time, don't execute sql
08-13 16:23:25.854 org.hibernate.cache.internal.StandardQueryCache DEBUG - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache
08-13 16:23:25.855 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2
08-13 16:23:25.855 org.hibernate.cache.internal.StandardQueryCache DEBUG - Checking query spaces are up-to-date: [test.stu]
08-13 16:23:25.855 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: test.stu
08-13 16:23:25.855 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - Element for key test.stu is null
08-13 16:23:25.855 org.hibernate.cache.internal.StandardQueryCache DEBUG - Returning cached query results
08-13 16:23:25.863 org.hibernate.engine.internal.StatisticalLoggingSessionEventListener INFO  - Session Metrics {
    8554 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    0 nanoseconds spent preparing 0 JDBC statements;
    0 nanoseconds spent executing 0 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    2024852 nanoseconds spent performing 7 L2C hits;
    233966 nanoseconds spent performing 1 L2C misses;
    432003 nanoseconds spent executing 1 flushes (flushing a total of 6 entities and 0 collections);
    8127 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)
}
-------------------------------------
zhs
zhs
lisi
08-13 16:23:25.866 org.hibernate.engine.internal.StatisticalLoggingSessionEventListener INFO  - Session Metrics {
    0 nanoseconds spent acquiring 0 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    0 nanoseconds spent preparing 0 JDBC statements;
    0 nanoseconds spent executing 0 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    68864 nanoseconds spent performing 2 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
-------------------------------------
08-13 16:23:25.866 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000161: Logging statistics....
08-13 16:23:25.866 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000251: Start time: 1439454205398
08-13 16:23:25.866 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000242: Sessions opened: 3
08-13 16:23:25.866 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000241: Sessions closed: 3
08-13 16:23:25.867 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000266: Transactions: 2
08-13 16:23:25.867 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000258: Successful transactions: 2
08-13 16:23:25.867 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000187: Optimistic lock failures: 0
08-13 16:23:25.867 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000105: Flushes: 2
08-13 16:23:25.867 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000048: Connections obtained: 2
08-13 16:23:25.867 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000253: Statements prepared: 1
08-13 16:23:25.868 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000252: Statements closed: 0
08-13 16:23:25.868 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000239: Second level cache puts: 6
08-13 16:23:25.868 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000237: Second level cache hits: 8
08-13 16:23:25.868 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000238: Second level cache misses: 0
08-13 16:23:25.868 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000079: Entities loaded: 6
08-13 16:23:25.868 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000080: Entities updated: 0
08-13 16:23:25.869 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000078: Entities inserted: 0
08-13 16:23:25.869 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000076: Entities deleted: 0
08-13 16:23:25.869 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000077: Entities fetched (minimize this): 0
08-13 16:23:25.869 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000033: Collections loaded: 0
08-13 16:23:25.869 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000036: Collections updated: 0
08-13 16:23:25.869 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000035: Collections removed: 0
08-13 16:23:25.869 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000034: Collections recreated: 0
08-13 16:23:25.870 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000032: Collections fetched (minimize this): 0
08-13 16:23:25.870 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000438: NaturalId cache puts: 0
08-13 16:23:25.870 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000439: NaturalId cache hits: 0
08-13 16:23:25.870 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000440: NaturalId cache misses: 0
08-13 16:23:25.870 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000441: Max NaturalId query time: 0ms
08-13 16:23:25.871 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000442: NaturalId queries executed to database: 0
08-13 16:23:25.871 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000210: Queries executed to database: 1
08-13 16:23:25.871 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000215: Query cache puts: 1
08-13 16:23:25.871 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000433: update timestamps cache puts: 0
08-13 16:23:25.871 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000434: update timestamps cache hits: 0
08-13 16:23:25.871 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000435: update timestamps cache misses: 1
08-13 16:23:25.872 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000213: Query cache hits: 1
08-13 16:23:25.872 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000214: Query cache misses: 1
08-13 16:23:25.872 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000173: Max query time: 67ms
08-13 16:23:25.885 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl INFO  - HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]

Scenario2:
開啟查詢快取,關閉二級快取。

執行結果:

Search Stu first time.
08-13 17:10:58.560 org.hibernate.cache.internal.StandardQueryCache DEBUG - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache
08-13 17:10:58.560 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2
08-13 17:10:58.560 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - Element for key sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2 is null
08-13 17:10:58.561 org.hibernate.cache.internal.StandardQueryCache DEBUG - Query results were not found in cache
Hibernate: 
    select
        stu0_.sno as sno1_2_,
        stu0_.sname as sname2_2_,
        stu0_.sex as sex3_2_,
        stu0_.age as age4_2_,
        stu0_.sdept as sdept5_2_ 
    from
        test.stu stu0_
08-13 17:10:58.620 org.hibernate.cache.internal.StandardQueryCache DEBUG - Caching query results in region: org.hibernate.cache.internal.StandardQueryCache; timestamp=5896016112107520
08-13 17:10:58.621 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2 value: [5896016112107520, 1, 2, 3, 4, 6, 7]
-------------------------------------
Search Stu second time, don't execute sql
08-13 17:10:58.632 org.hibernate.cache.internal.StandardQueryCache DEBUG - Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache
08-13 17:10:58.632 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: sql: select stu0_.sno as sno1_2_, stu0_.sname as sname2_2_, stu0_.sex as sex3_2_, stu0_.age as age4_2_, stu0_.sdept as sdept5_2_ from test.stu stu0_; parameters: ; named parameters: {}; transformer: [email protected]110f2
08-13 17:10:58.632 org.hibernate.cache.internal.StandardQueryCache DEBUG - Checking query spaces are up-to-date: [test.stu]
08-13 17:10:58.632 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - key: test.stu
08-13 17:10:58.632 org.hibernate.cache.ehcache.internal.regions.EhcacheGeneralDataRegion DEBUG - Element for key test.stu is null
08-13 17:10:58.633 org.hibernate.cache.internal.StandardQueryCache DEBUG - Returning cached query results
-------------------------------------
zhs
zhs
lisi
-------------------------------------
08-13 17:10:58.641 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000161: Logging statistics....
08-13 17:10:58.641 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000251: Start time: 1439457058255
08-13 17:10:58.641 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000242: Sessions opened: 0
08-13 17:10:58.641 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000241: Sessions closed: 0
08-13 17:10:58.641 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000266: Transactions: 0
08-13 17:10:58.642 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000258: Successful transactions: 0
08-13 17:10:58.642 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000187: Optimistic lock failures: 0
08-13 17:10:58.642 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000105: Flushes: 0
08-13 17:10:58.642 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000048: Connections obtained: 0
08-13 17:10:58.642 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000253: Statements prepared: 0
08-13 17:10:58.642 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000252: Statements closed: 0
08-13 17:10:58.642 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000239: Second level cache puts: 0
08-13 17:10:58.643 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000237: Second level cache hits: 0
08-13 17:10:58.643 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000238: Second level cache misses: 0
08-13 17:10:58.643 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000079: Entities loaded: 0
08-13 17:10:58.643 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000080: Entities updated: 0
08-13 17:10:58.643 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000078: Entities inserted: 0
08-13 17:10:58.643 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000076: Entities deleted: 0
08-13 17:10:58.644 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000077: Entities fetched (minimize this): 0
08-13 17:10:58.644 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000033: Collections loaded: 0
08-13 17:10:58.644 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000036: Collections updated: 0
08-13 17:10:58.644 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000035: Collections removed: 0
08-13 17:10:58.644 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000034: Collections recreated: 0
08-13 17:10:58.644 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000032: Collections fetched (minimize this): 0
08-13 17:10:58.645 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000438: NaturalId cache puts: 0
08-13 17:10:58.645 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000439: NaturalId cache hits: 0
08-13 17:10:58.645 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000440: NaturalId cache misses: 0
08-13 17:10:58.645 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000441: Max NaturalId query time: 0ms
08-13 17:10:58.645 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000442: NaturalId queries executed to database: 0
08-13 17:10:58.645 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000210: Queries executed to database: 0
08-13 17:10:58.646 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000215: Query cache puts: 0
08-13 17:10:58.646 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000433: update timestamps cache puts: 0
08-13 17:10:58.646 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000434: update timestamps cache hits: 0
08-13 17:10:58.646 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000435: update timestamps cache misses: 0
08-13 17:10:58.647 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000213: Query cache hits: 0
08-13 17:10:58.647 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000214: Query cache misses: 0
08-13 17:10:58.647 org.hibernate.stat.internal.ConcurrentStatisticsImpl INFO  - HHH000173: Max query time: 0ms
08-13 17:10:58.656 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl INFO  - HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/test]

從兩次的執行結果的日誌對比發現,Scenario1中的第二次查詢使用的是二級快取中的快取,從日誌中也可以看到命中的次數。Scenario2中第二次查詢使用的是查詢快取中的快取。

相關推薦

hibernate二級快取測試

SessionFactory類: import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; imp

Hibernate 二級快取和查詢快取

一級快取:     1,在session上面有一個一級快取;一級快取的生命週期和session相同,一級快取最大生命週期就是一個執行緒;在web環境下面,session的最大生命週期就是一次請求;     2,一級快取可以用來幹嘛? &nb

Hibernate 二級快取的作用

使用快取,是需要對應用系統進行效能優化而常採用的一種重要手段。合理地運用快取,可以極大的提高應用系統的執行效率。 Hibernate中應用快取:因為應用程式訪問資料庫,讀寫資料的代價非常高,而利用持久層的快取可以減少應用程式與資料庫之間的互動,即把訪問過的資料儲存到快取中,應用程式再次訪問已經訪

Hibernate二級快取問題

相關概念和定義1、快取的意義把一些不常修改,但是又經常用的資料存放到記憶體中,這樣能減少與資料庫的互動,提升程式的效能 2、Hibernate中提供了兩級快取:第一級別的快取是Session級別的快取(比如說在呼叫get方法的時候,如果已經查詢過一次了,第二次就不會查了,而是直接返回session快取中已經

hibernate 二級快取和事務級別詳講

一、概述 這章總的分兩大塊來講解   第一大塊,hibernate的事務管理。對於hibernate的事務管理來說,如果之前學過資料庫的事務管理,那麼在這裡就順風順水了。如果沒學過,第一次遇到,那也沒關係,我會詳細解釋其中的內容。   第二大塊,hibernate的二級快取機制。這個看起

spring boot整合ehcache 2.x 用於hibernate二級快取

spring boot整合ehcache 2x 用於hibernate二級快取 專案依賴 Ehcache簡介 hibernate二級快取配置 ehcache配置檔案 ehcache事件監聽 註解方式使用二級快取 完整程式碼 本文將介紹如何在spring boot中整合ehcache作為hiberna

關於hibernate 二級快取 報錯問題

Maven   hibernate-ehcache 和hibernate-core版本需要一致 <dependency> <groupId>org.hibernate</groupId> <artifactId>h

Hibernate二級快取

Hibernate中沒有自己去實現二級快取,而是利用第三方的。簡單敘述一下配置過程,也作為自己以後用到的時候配置的一個參考。 1、我們需要加入額外的二級快取包,例如EHcache,將其包匯入。需要:ehcache-core-2.4.3.jar , hibernate-e

Hibernate二級快取實現的方式(在類中,在方法上實現的思路)

資料庫快取 - 幕布 資料庫快取兩種處理方式一種處理model,只查詢快取,不更新快取參考列子 使用者表的敏感詞列表思路專案pom檔案匯入hibernate-encache快取檔案建立

SSH開啟Hibernate二級快取

1、新增ehcache依賴: <!-- Hibernate二級快取依賴 --> <dependency> <groupId>org.hibernate</groupId> <artifac

Hibernate 二級快取-ehcache

1 新增依賴包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>

hibernate 二級快取

一、為什麼需要快取? 拉高程式效能 二、什麼樣的資料需要快取 很少被修改或根本不改的資料 業務場景比如:耗時較高的統計分析sql、電話賬單查詢sql等 三、 ehcache的特點 1 夠快 Ehcache的發行有一段時長了,經過幾年的努力和不計其數的效能測試,E

hibernate 二級快取和查詢快取原理和關係

一、hibernate的二級快取 如果開啟了二級快取,hibernate在執行任何一次查詢的之後,都會把得到的結果集放到快取中,快取結構可以看作是一個hash table,key是資料庫記錄的id,value是id對應的pojo物件。當用戶根據id查詢物件的時候(load、iterator方法),會首先在

hibernate二級快取 Ehcache配置詳解

一、hibernate快取簡介 一級快取(session):內部快取 事務範圍:快取只能被當前事務訪問。快取的生命週期依賴於事務的生命週期,當事務結束時,快取也就結束生命週期。 二級快取(sessionFactory): 快取被應用範圍內的所有事務共享。 這些事務

hibernate二級快取攻略(第二天)

查詢快取 首先需要配置 hibernate.cache.use_query_cache = true  如果用ehcache,配置ehcache.xml,注意hibernate3.0以後不是net.sf的包名了 < cache name = " net.sf.

Hibernate二級快取以及ehcache的搭建配置

前言         這次主要複習Hibernate的二級快取的相關知識,配置以及使用。二級快取主要採用第三方的ehcache,也將介紹ehcache快取的相關配置屬性以及在專案中的搭建,具體的專案檢視下一篇的 Maven搭建SpringMVC+Hibernate專案詳解

hibernate二級快取機制

二級快取不像一級快取那樣預設開啟的,它需要配置。二級快取中的資料可適用範圍是當前應用的所有會話 在這些情況下面應該使用二級快取: 1.很少被修改的資料。如果經常修改的話要修改資料庫而且要修改快取裡面

Hibernate二級快取併發說明

二級快取的策略        當多個併發的事務同時訪問持久化層的快取中的相同資料時,會引起併發問題,必須採用必要的事務隔離措施。        在程序範圍或叢集範圍的快取,即第二級快取,會出現併發問題。因此可以設定以下4種類型的併發訪問策略,每一種策略對應一種事務隔離級別。

Java程式設計師從笨鳥到菜鳥之(七十七)細談Hibernate(十九)Hibernate二級快取詳解

歡迎關注微信賬號:java那些事:csh624366188.每天一篇java相關的文章 java交流工作群1: 77800592(已滿) java交流學生群2:234897635(已滿) java交流工作群3:94507287 java交流工作群4: 272265434 我的郵箱:

搭建Hibernate二級快取EHcache的環境

常見的快取元件 在預設情況下,hibernate會使用EHCache作為二級快取元件。但是,可以通過設定hibernate.cache.provider_class屬性,指定其他的快取策略,該快取策略必須實現org.hibernate.cache.Cach