我有这个 Ehcache XML 配置:
<ehcache>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskSpoolBufferSizeMB="30"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
此外,我还有一些包含实体的包(大约 150 个)。如果我在 tomcat 服务器上部署我的应用程序,日志中有很多 WARN 消息:
2015-04-29 11:59:02,712 [RMI TCP Connection(3)-127.0.0.1] WARN org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [com.company.project.entity.package.MyEntity]; using defaults.
我可以为每个实体编写配置 -
<cache name="com.company.project.entity.package.MyEntity"
maxEntriesLocalHeap="50"
eternal="false"
overflowToDisk="false"
timeToLiveSeconds="120">
<persistence strategy="localTempSwap"/>
</cache>
但是这样一来,我的配置文件就变得太大了(1600 行)。我认为还有另一种方法可以为每个实体设置默认配置并终止警告消息,但我不知道该怎么做。如果有人知道,请帮助。非常感谢。
最佳答案
这是记录警告消息的 Hibernate EHCache 缓存区域代码:
private Ehcache getCache(String name) throws CacheException {
try {
Ehcache cache = manager.getEhcache( name );
if ( cache == null ) {
LOG.unableToFindEhCacheConfiguration( name );
manager.addCache( name );
cache = manager.getEhcache( name );
LOG.debug( "started EHCache region: " + name );
}
HibernateEhcacheUtils.validateEhcache( cache );
return cache;
}
catch (net.sf.ehcache.CacheException e) {
throw new CacheException( e );
}
}
如您所见,您有两个选择:
- 您可以在
ehcache.xml
文件中声明缓存区域。 您将“org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory”日志级别设置为错误:
对于 Log4j2:
<Logger name="org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory" level="error"> <AppenderRef ref="File"/> </Logger>
关于java - 如何在使用 Hibernate 时删除 Ehcache WARN 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29947671/