JPA(5)使用二級緩衝,jpa使用二級緩衝

來源:互聯網
上載者:User

JPA(5)使用二級緩衝,jpa使用二級緩衝

jpa的緩衝分為一級緩衝和二級緩衝,一級緩衝值得是會話層級的,而二級緩衝是跨會話層級的。

  使用二級緩衝,使用到了Ehcache,首先第一步需要在設定檔中配置使用了二級緩衝

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> 

注意:既然是二級緩衝。那麼當然需要知道緩衝哪些東西和使用什麼jpa產品(這是我自己理解的),所以這個配置必須在最後面,

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">    <persistence-unit name="jpa-1" transaction-type="RESOURCE_LOCAL">                                <!-- 配置使用什麼ORM產品 -->        <!--        需要注意的是:        1.實現的ORM策略是繼承 javax.persistence.spi.PersistenceProvide介面,        2如果項目中只有一個JPA實現產品,那麼不指定也是可以的                 -->        <provider>org.hibernate.ejb.HibernatePersistence</provider>                <!-- 添加持久化類 -->                <class>com.hotusm.commom.entity.User</class>        <class>com.hotusm.commom.entity.Phone</class>        <class>com.hotusm.commom.entity.FirstLover</class>        <class>com.hotusm.commom.entity.Teacher</class>        <class>com.hotusm.commom.entity.Student</class>        <class>com.hotusm.commom.entity.Food</class>                <!-- 添加二級緩衝的策略        ALL:所以的實體類都被緩衝        NONE:所以的實體都不被緩衝        ENABLE_SELECTIVE:標示了@Cacheable(true)的實體會被緩衝        DISABLE_SELECTIVE:緩衝除了標示@Cacheable(true)的實體        UNSPECIFIFD:預設值。JPA產品預設值將會使用,這個要看JPA的具體實現產品了                  -->        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>          <properties>            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>            <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa"/>            <property name="javax.persistence.jdbc.user" value="root"/>            <property name="javax.persistence.jdbc.password" value="1234"/>                        <!-- 配置jpa實現產品 -->            <property name="hibernate.show_sql" value="true"/>            <property name="hibernate.hbm2ddl.auto" value="update"/>            <property name="hibernate.format_sql" value="true"/>            <!-- 使用二級緩衝 -->            <property name="hibernate.cache.use_second_level_cache" value="true"/>            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>            <property name="hibernate.cache.use_query_cache" value="true"/>        </properties>    </persistence-unit>    </persistence>

,這樣我們就可以在實體類的上面配置@Cacheable來指定是否使用二級緩衝,

聯繫我們

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