hinernate二級緩衝的小結

來源:互聯網
上載者:User

【hibernate二級緩衝】

    *二級緩衝也稱為進程級的緩衝或SessinoFactory級的緩衝,二級緩衝可以被所有的session共用
    *二級緩衝的生命週期和SessionFactory的生命週期一致,SessionFactory可以管理緩衝。
    *二級緩衝也是緩衝實體物件的。
   
【二級緩衝的配置和使用】
    *將echcache.xml檔案拷貝到src(如果使用ehcache緩衝的話)
    *開啟二級緩衝,修改hibernate.cfg.xml:
        <property name="hibernate.cache.use_second_level_cache">true</property>
    *指定緩衝產品供應商,修改hibernate.cfg.xml檔案:
        <property name="hibernate.cache.provider_class">EhCacheProvider</property>
    *指定使用緩衝的類:
        一種方式,在類的對應檔中配置:
        <cache usage="read-only"/>
    還有另一種方式 是在hibernate.cfg.xml中進行配置的,建議使用第二種方式:
    <class-cache usage="read-only" class="myHibernate.Student"/>
    注意:read-only可以避免鎖的競爭問題,對於變化不太頻繁的資料可以提高效率,但read-only會使資料庫中的
    資料在所設定的有效期間內與緩衝不同步,設定緩衝生命週期在ehcache.xml中設定:
        <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />
        有效時間為120s

【一級緩衝session與二級緩衝的互動】
    設定CacheMode
     * CacheMode.NORMAL :從二級緩衝中讀、寫資料
     * CacheMode.GET : 從二級緩衝中讀取資料,僅在資料更新時對二級緩衝寫資料
     * CacheMode.PUT : 僅向二級緩衝寫資料,但不從二級緩衝度資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.