Dogrizz
Dogrizz

Reputation: 161

Hibernate 2nd level cache

Hi I've run into some problems with hibernate 2nd level cache. As cache provider I use ehcache.

Part of config from 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;

my 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

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

and that removes that faulty prefix big thx ruslan!

Upvotes: 8

Views: 33293

Answers (3)

vorp
vorp

Reputation: 51

Hibernate add prefix to cache names based on appname or value of property hibernate.cache.region_prefix

If You set this property for "" (empty string) then You have regions named exactly like name in hibernate config.

Upvotes: 5

ruslan
ruslan

Reputation: 805

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". And it's not defined in your's ehcache.xml configuration. 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(region = 'Kierunek', usage = CacheConcurrencyStrategy.READ_WRITE) 
public class Kierunek implements Serializable {
  // ....
}

And in ehcache.xml

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

Upvotes: 9

fforw
fforw

Reputation: 5491

EHCache needs a configuration that tells it how to cache the objects in your application (live time, cache type, cache size, caching behaviour etc). 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.

Upvotes: 0

Related Questions