Java 休眠二级缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/765192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate 2nd level cache
提问by Dogrizz
Hi I've run into some problems with hibernate 2nd level cache. 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;
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!
并删除了错误的前缀 big thx ruslan!
采纳答案by 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". 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.
恕我直言,您将获得为您的班级生成的区域名称。这个生成的名称“persistence.unit:unitName=pz2EAR.ear/pz2EJB.jar#pz2EJB.pl.bdsdev.seps.encje.Kierunek”。它没有在您的 ehcache.xml 配置中定义。它也在寻找预定义的名称,所以它不能使用默认区域。
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" />
回答by fforw
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.
EHCache 需要一个配置来告诉它如何缓存应用程序中的对象(生存时间、缓存类型、缓存大小、缓存行为等)。对于您尝试缓存的每个类,它都会尝试找到合适的缓存配置,如果失败则打印上面的错误。
See http://ehcache.sourceforge.net/documentation/configuration.htmlfor how to configure EHCache.
有关如何配置 EHCache,请参阅http://ehcache.sourceforge.net/documentation/configuration.html。
回答by vorp
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.
如果您将此属性设置为“”(空字符串),那么您的区域名称与休眠配置中的名称完全相同。