Hibernate之二級緩衝

來源:互聯網
上載者:User

標籤:

                                                        Hibernate之二級緩衝

一.簡介

  Gaving King曾經對別人說,hibernate最耀眼之處是hibernate的緩衝機制。Hibernate為了降低應用程式對物理資料來源的訪問,使用了緩衝機制。Hibernate緩衝包括一級緩衝和二級緩衝。一級緩衝又稱"Session的緩衝",是不能被卸載的。本文筆者為大家介紹的是Hibernate的二級緩衝。

二.二級緩衝

  Hibernate的二級緩衝又稱為"SessionFactory的緩衝",由於SessionFactory對象的生命週期和應用的整個過程對應,他是可選的,是一個可配置的外掛程式,預設情況下SessionFactory不會啟用這個外掛程式。Hibernate5.2提供了org.

hibernate.cache.ehcache.EhCacheRegionFactory類,它充當快取區外掛程式與Hibernate之間的適配器。

  那麼什麼樣的資料適合放在二級緩衝中呢?

  A.很少被修改的資料

  B.不是很重要的資料

  C.不會被並發訪問的資料

  C.常量資料

  那麼什麼樣的資料不適合放在二級緩衝中呢?

  A.經常被修改的資料

  B.絕對不允許出現並發訪問的資料。如財務資料,絕對不允許出現並發

  C.與其他應用共用的資料

三.二級緩衝的實現

3.1 hibernate.cfg.xml中加入以下配置

<!-- 是否開啟二級緩衝 --><property name="cache.use_query_cache">true</property>        <!-- 配置二級緩衝的所需的類 --><property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>        <!-- 配置啟用查詢快取--><property name="cache.use_query_cache">true</property>

3.2 POJO類的註解形式實現

@Entity@Table(name="emp")@Cacheable(value=true)@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)public class Employee {    @Id    @Column(name="empno")    @GenericGenerator(name="assignedGenerator", strategy="assigned")    @GeneratedValue(generator="assignedGenerator")    private int id;        @Column(name="ename")    private String ename;        @Column(name="job")    private String job;    @Column(name="hiredate")    private Date hiredate;        @Column(name="sal")    private Double salary;        @Column(name="comm")    private Double comm;        //setter and getter}

3.3 快取檔案

在src目錄下建立一個檔案,名字叫做encache.xml,檔案的類容如下

<ehcache>    <diskStore path="d:/hibernateCache"/>    <defaultCache        maxElementsInMemory="10"        eternal="false"        timeToIdleSeconds="120"        timeToLiveSeconds="120"        overflowToDisk="true"/>            <cache name="com.demo.hibernate.one2many.Employee"        maxElementsInMemory="10"        eternal="false"        timeToIdleSeconds="300"        timeToLiveSeconds="600"        overflowToDisk="true"/></ehcache>

3.4 測試

public class HibernateQBCTest {        private SessionFactory sf;        @Before    public void getSessionFactory(){        sf = OracleSessionUtils.getSessionFactory();    }        //查詢所有    @Test    public void list(){         Session session = sf.openSession();         Transaction tx = session.beginTransaction();         List<Employee> empList = session.createCriteria(Employee.class).list();         for(Employee e : empList){             System.out.println(e.getEname());         }         tx.commit();         session.close();                  //以下查詢不會發送SQL         Session session1 = sf.openSession();         Transaction tx1 = session1.beginTransaction();         Employee e = session1.get(Employee.class, 7369);         System.out.println(e.getEname());           tx1.commit();         session1.close();    }}

 

Hibernate之二級緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.