简体   繁体   English

Hibernate二级缓存

[英]Hibernate 2nd level cache

Hi I've run into some problems with hibernate 2nd level cache. 嗨,我遇到了一些hibernate二级缓存的问题。 As cache provider I use ehcache. 作为缓存提供程序,我使用ehcache。

Part of config from persistence.xml 来自persistence.xml的配置的一部分

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
<property name="hibernate.cache.provider_configuration_file_resource_path" value="/ehcache.xml" />

I configure my entities using annotations so: 我使用注释配置我的实体,所以:

@Cache(region = "Kierunek", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Kierunek implements Serializable {

imports for those annotations are: import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; 这些注释的import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy;是: import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy;

my ehcache.xml 我的ehcache.xml

<diskStore path="java.io.tmpdir" />

<defaultCache maxElementsInMemory="10000" eternal="false"
    timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
    diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
    diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU" />

<cache name="Kierunek" maxElementsInMemory="1000"
    eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LRU" />

And anyone idea why i get following error ? 任何人都知道为什么我会得到以下错误?

WARNING: Could not find a specific ehcache configuration for cache named [persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.Kierunek]; using defaults.
19:52:57,313 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB state=Create
java.lang.IllegalArgumentException: Cache name cannot contain '/' characters.

solution is to add another property to persistence.xml 解决方案是向persistence.xml添加另一个属性

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

and that removes that faulty prefix big thx ruslan! 并删除那个错误的前缀大thx ruslan!

IMHO, you get the generated region name for your class. 恕我直言,你得到你班级的生成区域名称。 This generated name "persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje.Kierunek". 生成的名称为“persistence.unit:unitName = pz2EAR.ear / pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje.Kierunek”。 And it's not defined in your's ehcache.xml configuration. 并且它没有在您的ehcache.xml配置中定义。 Also it's looking for the predefined name, so it can't use default region. 它还在寻找预定义的名称,因此它不能使用默认区域。

As an option to solve this problem you can use @Cache annotation properties to predefine some region name, like 作为解决此问题的选项,您可以使用@Cache批注属性来预定义某些区域名称,例如

@Cache(region = 'Kierunek', usage = CacheConcurrencyStrategy.READ_WRITE) 
public class Kierunek implements Serializable {
  // ....
}

And in ehcache.xml 并在ehcache.xml中

<cache name="Kierunek" 
       maxElementsInMemory="1000"
       eternal="true" 
       overflowToDisk="false" 
       memoryStoreEvictionPolicy="LRU" />

Hibernate add prefix to cache names based on appname or value of property hibernate.cache.region_prefix Hibernate根据appname或属性值hibernate.cache.region_prefix为缓存名称添加前缀

If You set this property for "" (empty string) then You have regions named exactly like name in hibernate config. 如果您将此属性设置为“”(空字符串),那么您在hibernate配置中具有与名称完全相同的区域。

EHCache needs a configuration that tells it how to cache the objects in your application (live time, cache type, cache size, caching behaviour etc). EHCache需要一个配置,告诉它如何缓存应用程序中的对象(实时,缓存类型,缓存大小,缓存行为等)。 For every class you try to cache it will try to find an appropriate cache configuration and print the error above if it fails to do so. 对于您尝试缓存的每个类,它将尝试查找适当的缓存配置,并在未能执行此操作时打印上述错误。

See http://ehcache.sourceforge.net/documentation/configuration.html for how to configure EHCache. 有关如何配置EHCache的信息,请参见http://ehcache.sourceforge.net/documentation/configuration.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM