首页 文章

Hibernate二级缓存

提问于
浏览
8

嗨,我遇到了一些hibernate二级缓存的问题 . 作为缓存提供程序,我使用ehcache .

来自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" />

我使用注释配置我的实体,所以:

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

这些注释的导入是: import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy;

我的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" />

谁知道为什么我会得到以下错误?

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.

解决方案是向persistence.xml添加另一个属性

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

并删除那个错误的前缀大thx ruslan!

3 回答

  • 5

    恕我直言,你得到你 class 的生成区域名称 . 生成的名称为“persistence.unit:unitName = pz2EAR.ear / pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje.Kierunek” . 并且它没有在您的ehcache.xml配置中定义 . 它还在寻找预定义的名称,因此它不能使用默认区域 .

    作为解决此问题的选项,您可以使用@Cache批注属性来预定义某些区域名称,例如

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

    并在ehcache.xml中

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

    Hibernate根据appname或属性值hibernate.cache.region_prefix为缓存名称添加前缀

    如果您将此属性设置为“”(空字符串),那么您在hibernate配置中具有与名称完全相同的区域 .

  • 9

    EHCache需要一个配置,告诉它如何缓存应用程序中的对象(实时,缓存类型,缓存大小,缓存行为等) . 对于您尝试缓存的每个类,它将尝试查找适当的缓存配置,并在未能执行此操作时打印上述错误 .

    有关如何配置EHCache的信息,请参阅http://ehcache.sourceforge.net/documentation/configuration.html .

相关问题