1

I am trying to configure Redisson Hibernate 2L cache in Spring MVC project. I had follow tutorials, although most of them is devoted to Spring Boot. I have added dependencies in pom.xml file:

<dependency>
  <groupId>org.redisson</groupId>
  <artifactId>redisson</artifactId>
  <version>3.12.0</version>
</dependency>
<dependency>
  <groupId>org.redisson</groupId>
  <artifactId>redisson-hibernate-53</artifactId>
  <version>3.11.6</version>
</dependency>

In persistance.xml (second level cache is set to false for now, because I only try to make it start the application):

<property name="hibernate.cache.region.factory_class" value="org.redisson.hibernate.RedissonRegionFactory"/>
  <property name="hibernate.cache.use_second_level_cache" value="false"/>
  <property name="hibernate.cache.use_query_cache" value="false"/>

And in my Configuration file:

    @Bean(destroyMethod="shutdown")
  RedissonClient redisson() {
    Config config = new Config();
    config.useClusterServers()
            .addNodeAddress("redis://127.0.0.1:6379");
    return Redisson.create(config);
  }

  @Bean
  CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
    Map<String, CacheConfig> config = new HashMap<>();
    config.put("testMap",
            new CacheConfig(120000, 180000)); //2min TTL and 3min max idle time
    return new RedissonSpringCacheManager(redissonClient, config);
  }

Project is building correctly, but when I try to run it, I recieve an error:

Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.CacheImplementor]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:275)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
    at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.getService(SessionFactoryServiceRegistryImpl.java:97)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:238)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:938)
    at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:141)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390)
    ... 63 more
Caused by: java.lang.IllegalStateException: Cache provider not started
    at org.hibernate.cache.spi.AbstractRegionFactory.verifyStarted(AbstractRegionFactory.java:65)
    at org.hibernate.cache.spi.support.RegionFactoryTemplate.buildTimestampsRegion(RegionFactoryTemplate.java:66)
    at org.hibernate.cache.internal.EnabledCaching.<init>(EnabledCaching.java:80)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:33)
    at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:24)
    at org.hibernate.service.spi.SessionFactoryServiceInitiator.initiateService(SessionFactoryServiceInitiator.java:30)
    at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:61)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
    ... 72 more
Caused by: org.hibernate.cache.CacheException: Unable to locate Redisson configuration
    at org.redisson.hibernate.RedissonRegionFactory.createRedissonClient(RedissonRegionFactory.java:107)
    at org.redisson.hibernate.RedissonRegionFactory.prepareForUse(RedissonRegionFactory.java:83)
    at org.hibernate.cache.spi.AbstractRegionFactory.start(AbstractRegionFactory.java:91)
    at org.hibernate.cache.internal.EnabledCaching.<init>(EnabledCaching.java:77)

I do not know what I'm missing in order to locate Redisson configuration..? I would appreciate any help!

gariaable
  • 179
  • 1
  • 3
  • 15

3 Answers3

3

You forgot to define path to Redisson config:

<property name="hibernate.cache.redisson.config" value="/redisson.yaml" />
Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71
  • There's no way to avoid this extra yaml configuration and initialize a `RedissionClient` in Java code instead ? – Blockost Dec 09 '21 at 18:29
1

To initialize a RedissionClient in Java code you need to create a CustomRegionFactory extending RedissonRegionFactory, then override the method createRedissonClient to create your RedissonClient like this:

final Config redissonConfig = new Config();
       redissonConfig.useSingleServer()
               .setKeepAlive(true)
               .setConnectTimeout(1000*3)
               .setRetryAttempts(5)
               .setDnsMonitoringInterval(1000*8)
               .setAddress("redis://127.0.0.1:6379");
       return Redisson.create(redissonConfig);  

Then you need to specify your CustomRegionFactory for hibernate.cache.region.factory_class

Hope that helps!

User01
  • 45
  • 7
0

It is expecting redisson configuratio file https://github.com/redisson/redisson/wiki/2.-Configuration

Payal Bansal
  • 725
  • 5
  • 17
  • Yes, it is and first subsection of this tutorial is an approach with programmatic configuration, which is exactly the same as I have in my configuration class above (Look at @Bean RedissonClient redisson()). – gariaable Jan 03 '20 at 08:43
  • Hey, did you find a way to do it programmatically ? – Blockost Dec 09 '21 at 18:28