views:

4562

answers:

3

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!

A: 

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.

fforw
I have proper configuration of ehcache (and you don't need too config each entity in ehcache.xml if you have "default" cache defined).The problem is why does hibernate generates that weird cache name?It should be pl.bdsdev.seps.encje.Kierunek and is persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje.Kierunek
Dogrizz
+3  A: 

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" />
ruslan
yes I do get generated region name (now I know it is region, in article I've read it was just called cache so I got confused..) and that was my original intention. Wonder why Hibernate generates faulty region name.And once again if u have default entry you don't need to specify region per entity (if you are ok with that).Anyway i'm gratefull for the answere. It solved my problem. Thanks! ^^
Dogrizz
Welcome. I've checked the documentation. They said that default region name is a fully qualified class name. This a big mistake in the documentation, they should fix it ASAP, as it's not clear in any case.
ruslan
it worked for 2 deploymnets and now i get: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=Createjava.lang.IllegalArgumentException: Cache name cannot contain '/' characters.It sees region name set in the annotation but adds that spare prefix..
Dogrizz
What hibernate version are you using? Maybe you'll need next cache provider class for ehcache org.hibernate.cache.EhCacheProvider.We have used next hibernate property hibernate.cache.region_prefix, e.g. <property name="hibernate.cache.region_prefix" value=""/>Try this. Hope it will help.
ruslan
<property name="hibernate.cache.region_prefix" value=""/>that solved the problem. big thanks ^^
Dogrizz
A: 

You may please refer to the following link, and do let me know if it really helped http://freejavaclass.com/articles/articleshome.jsp?tutorialpath=hibernate

athi