title: Using different Redis databases for Spring sessions and custom data
body: <p>In my REST controllers Spring project, I want to store Session information in Redis and my custom information (whatever I need) in Redis also, but in a separate database.</p>
  
  <p>In my <em>application.properties</em> I have defined the following:</p>
  
  <pre><code>spring.session.store-type=redis
  spring.session.redis.namespace=rdrestcore
  spring.redis.host=192.168.201.46
  spring.redis.port=6379
  spring.redis.database=5
  
  com.xyz.redis.host=192.168.201.46
  com.xyz.redis.db=0
  com.xyz.redis.port=6379
  com.xyz.redis.pool.min-idle=5
  </code></pre>
  
  <p>I also have a class with the following code (I read somewhere I needed this class just to let Spring administer sessions in Redis... as you can see, it has no code):</p>
  
  <pre><code>@Configuration
  @EnableRedisHttpSession 
  public class SessionConfig extends AbstractHttpSessionApplicationInitializer 
  {}
  </code></pre>
  
  <p>I finally have a redis connection factory like this:</p>
  
  <pre><code>@Configuration
  @EnableRedisRepositories
  public class RdRedisConnectionFactory {
      @Autowired 
      private Environment env;
      @Value("${com.xyz.redis.host}")
      private String redisHost;
      @Value("${com.xyz.redis.db}")
      private Integer redisDb;
      @Value("${com.xyz.redis.port}")
      private Integer redisPort;
      @Value("${com.xyz.redis.pool.min-idle}")
      private Integer redisPoolMinIdle;
  
      @Bean
      JedisPoolConfig jedisPoolConfig() {
          JedisPoolConfig poolConfig = new JedisPoolConfig();        
          if(redisPoolMinIdle!=null) poolConfig.setMinIdle(redisPoolMinIdle);
  
          return poolConfig;
      }
  
      @Bean    
      JedisConnectionFactory jedisConnectionFactory() {
          JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
          if(redisHost!=null) jedisConFactory.setHostName(redisHost);
          if(redisPort!=null) jedisConFactory.setPort(redisPort);
          if(redisDb!=null) jedisConFactory.setDatabase(redisDb);
          jedisConFactory.setPoolConfig(jedisPoolConfig());
          return jedisConFactory;
      }
  
      @Bean
      public RedisTemplate&lt;String, Object&gt; redisTemplate() {
          final RedisTemplate&lt; String, Object &gt; template =  new RedisTemplate();
          template.setConnectionFactory( jedisConnectionFactory() );
          template.setKeySerializer( new StringRedisSerializer() );
          template.setHashValueSerializer( new StringRedisSerializer() );
          template.setValueSerializer( new StringRedisSerializer() );
          return template;
      }
  }
  </code></pre>
  
  <p>And when I need a Redis read or write, I just autowire <em>redisTemplate</em> wherever I need it and it works. </p>
  
  <p>Now... the problem: Spring session info gets stored in the same database as my custom information, although I specified database 5 for session information and database 0 for my custom information. Everything goes to database 0.</p>
  
  <p>Does anyone know what Im doing wrong?</p>
  
  <p>Thank you</p>