Uploaded image for project: 'Spring Data Redis'
  1. Spring Data Redis
  2. DATAREDIS-863

RedisTemplate doesn't ensure JedisConnectionFactory is correctly initialized

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None
    • Environment:
      spring-boot-starter-redis 1.4.7.RELEASE

      Description

      Steps to reproduce:

      Consider the following code:

      @Bean
      public RedisWrapper redisOF(@Qualifier("ofConfig") RedisProperties props) {
          return createRedisInstance(props);
      }
      
      @Bean
      public RedisWrapper redisUS(@Qualifier("usConfig") RedisProperties props) {
          return createRedisInstance(props);
      }
      
      private RedisWrapper createRedisInstance(RedisProperties props) {
              RedisTemplate<String, Object> redisTemplateObject = new RedisTemplate<>();
              redisTemplateObject.setConnectionFactory(createRedisConnectionFactory(props));
              setSerializerString(redisTemplateObject);
              //redisTemplateObject.afterPropertiesSet();
              return new RedisWrapper(redisTemplateObject);
      }
      
      private RedisConnectionFactory createRedisConnectionFactory(RedisProperties redisProperties) {
              JedisPoolConfig poolConfig = new JedisPoolConfig();
              // Omit codes that configure JedisPoolConfig via properties
              JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig);
              // Omit codes that configure RedisConnectionFactory via properties
              //connectionFactory.afterPropertiesSet();
              return connectionFactory;
      }

       Then use RedisWrapper bean to access redis data.

      Expected:

      RedisTemplate reports error when a uninitialized ConnectionFactory is set.

      or

      RedisTemplate automatically initialize (call  ConnectionFactory.afterPropertiesSet() ) the supplied ConnectionFactory is not initialized (generally not a bean).

      Actual:

      These code will code NPE while reading data from Redis, since the pool in JedisConnectionFactory is not initialized by BeanFactory. (RedisConnectionFactory is not a bean). Developers won't know this before they actually access redis data.

      By calling  connectionFactory.afterPropertiesSet() and redisTemplateObject.afterPropertiesSet() will fix this problem. However, this delays discovery of configuration error to call time.

      No document says that afterPropertiesSet() must be called when RedisConnectionFactory in created manually.

      Workaround:

      • Developer call afterPropertiesSet() after initiate RedisTemplate and ConnectionFactory.
      • Developer implements InitializingBean and manually call  afterPropertiesSet() for RedisTemplate and ConnectionFactory.
      • Document ConnectionFactory and RedisTemplate, tell them to call afterPropertiesSet() in such case.

        Attachments

          Activity

            People

            • Assignee:
              mp911de Mark Paluch
              Reporter:
              chzhong chzhong
              Last updater:
              Mark Paluch
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: