2009-04-19 141 views
8

您好我遇到了休眠二級緩存的一些問題。 作爲緩存提供者,我使用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魯斯蘭!

+1

你應該完全拼出單詞「level」而不是「lvl」。它更具可讀性和可讀性。 – 2009-04-19 19:29:33

+1

你說得對。我正在寫這篇文章,感覺很沮喪,所以我沒有想過這件事^^謝謝 – Dogrizz 2009-04-19 20:38:59

回答

8

恕我直言,你會得到你的班級生成的區域名稱。這生成名稱「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

的EHCache需要告訴它如何緩存在應用程序中的對象(直播時間,緩存類型,緩存大小,緩存行爲等)的配置。對於您嘗試緩存的每個類,它都會嘗試找到適當的緩存配置,並在上面的錯誤中輸出錯誤。

http://ehcache.sourceforge.net/documentation/configuration.html關於如何配置的EHCache。

+0

我有適當的ehcache配置(如果你有「默認」緩存定義,你不需要在ehcache.xml中配置每個實體)。 問題是爲什麼hibernate生成奇怪的緩存名稱? 應當pl.bdsdev.seps.encje.Kierunek並且persistence.unit:的unitName = pz2EAR.ear/pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje。Kierunek – Dogrizz 2009-04-19 12:27:25

5

休眠添加前綴緩存名基於應用程序的名字或財產hibernate.cache.region_prefix

的價值。如果你設置該屬性爲「」(空字符串),那麼你已經命名的區域酷似名稱在休眠配置。