spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

@EnableRedisRepositories cannot be configured to use their own RedisTemplate beans

Open aem-quotient opened this issue 4 years ago • 8 comments

I have a use case like using two different regions Redis. Since the Redis is not synced between two different data centers. Need to update the data center in a synchronized fashion.

I'm facing a problem while updating the cache, The cache is updated in only one region.

@EnableRedisRepositories(basePackageClasses = {RefreshEastCacheRepository.class}, redisTemplateRef = "refreshEastRedisTemplate")
@EnableRedisRepositories(basePackageClasses = {RefreshWestCacheRepository.class}, redisTemplateRef = "refreshWestRedisTemplate")

I have configured the two different Redis in the same project and in one of the configurations I have added @primary(East Region) for the base use-case.

@repository("eastRepository")
public interface RefreshEastCacheRepository<T, D> extends CrudRepository<T, D>, QueryByExampleExecutor {
}
@repository("westRepository")
public interface RefreshWestCacheRepository<T, D> extends CrudRepository<T, D>, QueryByExampleExecutor {
}

I have Autowired the above repository in my class. When there is any update happened, the cache is updated only in the east region. but it's not updating in the west. If we use RedisTemplate we can update in the two different regions. But I feel comfortable using the repository.save method to update in the cache.

When I dig deeper into the code the container factory is the default for all the Repository and it's the same for all the Repositories.

I need a way to differentiate the two different containers at the Repository level. In the @EnableJPARepository, it allows us to add the EntityBeanFactory but in the EnableRedisRepositories we don't have an option to point out the right container factory.

Is there any better way to do that ???

aem-quotient avatar Jan 19 '21 09:01 aem-quotient

Note that @Repository has no effect on Spring Data repositories as @Repository is a component annotation to be used with Java classes, not interfaces.

The issue is caused by RedisRepositoryConfigurationExtension that configures several beans with the same name and backs off if there is already a bean registered with a particular name. In this case, RedisKeyValueAdapter is configured with the first RedisTemplate and reused for both repositories.

For now, there's no simple workaround. You need to spin up your repositories via RedisRepositoryFactory and wire all beans (RedisKeyValueTemplate, RedisKeyValueAdapter, RedisMappingContext) yourself to create an isolated repository per Template.

I'm going to take this issue to the team for further discussion.

mp911de avatar Jan 19 '21 10:01 mp911de

One more thing: RedisReactiveAutoConfiguration is part of Spring Boot. Can you file there a ticket that RedisReactiveAutoConfiguration should be only activated if there's a single ReactiveRedisConnectionFactory bean?

mp911de avatar Jan 19 '21 10:01 mp911de

Sure I will the ticket there as well

aem-quotient avatar Jan 19 '21 10:01 aem-quotient

@mp911de Any update on this ??

aem-quotient avatar Jan 19 '21 19:01 aem-quotient

Tried the above approach which was suggested by you. But no luck.

aem-quotient avatar Jan 19 '21 19:01 aem-quotient

Related to spring-projects/spring-data-keyvalue#363

mp911de avatar Mar 18 '21 09:03 mp911de