3

I am using spring-data-redis on my spring-boot 1.4 application. I have two distinct CrudRepositories. However, I am struggling to associate them with their respective Connection factories.

Bottom line is: I'd like PersonRedisRepository to use db #6 and OtherPurposeRedisRepository to use db #3. To be hoehest, I am not 100% sure if the way I am tackling the matter is correct.


The repository

interface PersonRedisRepository extends CrudRepository<Person, String> {

}

interface OtherPurposeRedisRepository extends CrudRepository<OtherPurpose, String> {

}

Configuration for person repository

@EnableRedisRepositories(basePackageClasses = [PersonRedisRepository.class], redisTemplateRef = "personRedisTemplate")
class RedisConfigurationForPerson {

@Bean(name = "personFactory")
public RedisConnectionFactory personJedisConnectionFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory()
    jedisConnectionFactory.usePool = true
    jedisConnectionFactory.hostName = "127.0.0.1"
    jedisConnectionFactory.database = 6

    return jedisConnectionFactory
}

@Bean(name = "personRedisTemplate")
public RedisTemplate<byte[], byte[]> availabilityCacheRedisTemplate() {
    RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>()
    template.setConnectionFactory(personJedisConnectionFactory())

    template
}
}

Configuration for other purpose repository

@EnableRedisRepositories(basePackageClasses = [OtherPurpsoseRepository.class], redisTemplateRef = "otherPurposeRedisTemplate")
class RedisConfigurationForOtherPurpose {

@Bean(name = "otherPurposeFactory")
public RedisConnectionFactory otherPurposeJedisConnectionFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory()
    jedisConnectionFactory.usePool = true
    jedisConnectionFactory.hostName = "127.0.0.1"
    jedisConnectionFactory.database = 3

    return jedisConnectionFactory
}

@Bean(name = "otherPurposeRedisTemplate")
public RedisTemplate<byte[], byte[]> otherPurposeRedisTemplate() {
    RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>()
    template.setConnectionFactory(otherPurposeJedisConnectionFactory())

    template
}
}

Everything works just fine, I can read/write using both Repositories. However, they both read/write on the db 6.

Jeff Cook
  • 5,050
  • 13
  • 65
  • 127
guisilva
  • 59
  • 1
  • 6

1 Answers1

2

Another guy had the same problem as you. Even if the examples are for jpa repositories these links should help you :

Spring Boot Configure and Use Two DataSources

http://www.baeldung.com/spring-data-jpa-multiple-databases

you have first to bind the configuration datasource with the @Primay annotation and specify the datasource you are working on. This is the first part. I've looked quickly the second part and I will go deeper later. Will update my psot when done ;)

Community
  • 1
  • 1
belkor
  • 131
  • 1
  • 5
  • Thanks for your feedback, I have already been to those pages but it seems I was able to track the issue. For some reason, it only reads the annotation @EnableRedisRepositories property "redisTemplateRef" from the last annotated class. In this case, "RedisConfigurationForOtherPurpose". How I did it? I changed the values of "redisTemplateRef" to some dummy string, and spring boot failed to start only when it could not find the "redisTemplateRef" of class "RedisConfigurationForOtherPurpose" – guisilva Apr 12 '17 at 03:43
  • Yes it seems the method on Baeldung differs from yours. Maybe you should try this way. UnformtunatIy I do not have the time to test this before next week, if you still have this problem I will try to reproduce it on my side ;) – belkor Apr 13 '17 at 07:15