Spring Boot - Ehcache二级缓存集成

时间:2017-09-28 10:48:23

标签: java spring hibernate caching

我试图在二级缓存中保存一些变量。我已阅读并应用了instructions in Baeldung。但是,它一直给出这个错误:

引起:org.hibernate.cache.NoCacheRegionFactoryAvailableException:在应用程序中使用了二级缓存,但未给出属性hibernate.cache.region.factory_class;请使用hibernate.cache.region.factory_class设置禁用二级缓存或设置正确的区域工厂,并确保类路径上有二级缓存提供程序(例如,hibernate-infinispan)。     在org.hibernate.cache.internal.NoCachingRegionFactory.buildTimestampsRegion(NoCachingRegionFactory.java:88)     在org.hibernate.cache.spi.UpdateTimestampsCache。(UpdateTimestampsCache.java:57)     在org.hibernate.internal.CacheImpl。(CacheImpl.java:53)     在org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:28)     在org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:20)     at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:49)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)     在org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)     at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:68)     在org.hibernate.internal.SessionFactoryImpl。(SessionFactoryImpl.java:244)     在org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)     在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)

我将此依赖项添加到pom.xml:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>5.2.2.Final</version>
</dependency>

然后,将这些行添加到application.properties:

hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.use_query_cache=true
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

在实体类的顶部,我添加了这些缓存注释:

@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)

在配置文件中,我添加了 @EnableCaching 注释。

以下是我添加到配置文件中的其他bean和方法:

@Bean
public DataSource dataSource() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("spring.datasource.driver-class-name")));
    dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("spring.datasource.url")));
    dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("spring.datasource.username")));
    dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("spring.datasource.password")));

    return dataSource;
}

final Properties additionalProperties() {
    final Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
    hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
    return hibernateProperties;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}

@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);
    return transactionManager;
}

配置方法有问题吗? 注意:这是一个spring-boot项目。

0 个答案:

没有答案