簡體   English   中英

使用Hibernate,EhCache和Wildlfy設置L2緩存的參數

[英]Set parameters for L2 cache with Hibernate, EhCache and Wildlfy

我有使用Hibernate和EhCache作為第二級緩存提供程序的應用程序。 該應用程序部署在Wildfly 8.2上。 二級緩存已配置並按預期工作,但是我無法弄清楚如何以通用方式為echache.xml配置中的二級緩存提供單獨的配置。 目前,我的設置如下:

Entitiy:

     /**
     * The Country class
     */
    @Entity
    @Table(name = "country")
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "ENTITY_L2_CACHE")
    public class Country extends AbstractPersistentEntity {}

pesistence.xml

<?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="app_PU" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>jdbc/app</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
            <property name="hibernate.transaction.jta.platform" value="com.torqueits.pos.jpa.ProxyJtaPlatform"/>
            <!-- enabling L2 cache -->
            <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
            <property name="hibernate.generate_statistics" value ="false" /> 
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="false"/>
            <property name="hibernate.cache.ehcache.statistics" value="false"/>
            <property name="hibernate.generate_statistics" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

還有ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" name="torqueCacheManager">
    <diskStore path="java.io.tmpdir"/>
    <!-- Fail safe default cache-->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        ...
        memoryStoreEvictionPolicy="LRU">        
    </defaultCache>
    <cache name="ENTITY_L2_CACHE"  
       maxElementsInMemory="10000"
       ...
       diskExpiryThreadIntervalSeconds="120"
       memoryStoreEvictionPolicy="LRU"/>        
</ehcache>

當我將實體配置為使用ENTITY_L2_CACHCE作為第二級緩存的區域時,休眠使用的實際名稱為

application.war#app_PU.ENTITY_L2_CACHE

它使用部署名稱加上持久性單元名稱作為區域前綴。 我無法控制部署名稱,因此無法將“ application.war#app_PU.ENTITY_L2_CACHE”放入ehcache.xml。 我不確定這是否與休眠或Wildfly服務器有關。

有什么方法可以為未綁定到特定部署名稱的二級緩存配置參數?

看着

org.hibernate.cfg.AvailableSettings

班,我發現

'hibernate.cache.region_prefix'

控制緩存區域前綴的參數。 因此,要解決此問題,我需要配置一些前綴,然后在ehCache.xml配置文件中將此前綴用於L2緩存。

該參數應在persistence.xml文件中設置:

<property name="hibernate.cache.region_prefix" value="com.example.app"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM