弹簧&x2B;冬眠+;c3p0+;ehcache java配置
我不熟悉spring、hibernate、c3p0和ehcache。除了web.xml之外,我正在完全使用Java配置开发一个应用程序。我必须在我的应用程序中使用二级缓存。所以我添加了以下代码弹簧&x2B;冬眠+;c3p0+;ehcache java配置,java,spring,hibernate,ehcache,c3p0,Java,Spring,Hibernate,Ehcache,C3p0,我不熟悉spring、hibernate、c3p0和ehcache。除了web.xml之外,我正在完全使用Java配置开发一个应用程序。我必须在我的应用程序中使用二级缓存。所以我添加了以下代码 import net.sf.ehcache.config.CacheConfiguration; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfi
import net.sf.ehcache.config.CacheConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer{
@Bean(destroyMethod="shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setName("myCacheName");
cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
cacheConfiguration.setMaxEntriesLocalHeap(1000);
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
config.addCache(cacheConfiguration);
return net.sf.ehcache.CacheManager.newInstance(config);
}
@Bean
@Override
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
}
@Override
public CacheResolver cacheResolver() {
return null;
}
@Override
public CacheErrorHandler errorHandler() {
return null;
}
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
}
在web.xml中,我将其添加为
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
ApplicationContextConfiguration
CachingConfig
</param-value>
</context-param>
当我尝试运行时,我遇到以下错误
org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
我明白我必须补充一点
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
net.sf.ehcache.hibernate.EhCacheRegionFactory
但不确定通过Java配置在何处以及如何添加它。有人能帮忙吗
此外,我是否需要添加以下内容
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
org.hibernate.cache.EhCacheProvider
真的
真的
假设您能够检索实体,并且只想启用二级缓存:
在现有的additionalProperties()
方法中添加以下属性
properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
properties.setProperty("hibernate.cache.use_second_level_cache", "true");
properties.setProperty("hibernate.cache.use_query_cache", "true");
在实体
上,您希望在该实体上启用二级缓存位置@cache
注释,例如:@cache(用法=cacheconcurrentystrategy.READ\u WRITE)
您可以删除
CachingConfig
类,因为该类与为Hibernate启用二级缓存无关。您的应用程序当前运行时没有二级缓存吗?我的意思是,您是否能够在没有二级缓存的情况下保存和检索实体?为什么要使用cachingconfiguer
?因为这与您所指的Hibernate二级缓存无关?@Madhusudana Reddy Sunnapu——是的,我可以在没有二级缓存的情况下进行检索
properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
properties.setProperty("hibernate.cache.use_second_level_cache", "true");
properties.setProperty("hibernate.cache.use_query_cache", "true");