简体   繁体   中英

RedisSystemException: Unknown redis exception; nested exception is java.lang.NullPointerException

Currently, i'm trying to learn spring data redis

I've create some test project and successfully insert the data, however i got an issue when using pipeline command

this is my config file :

public RedisTemplate<String, String> redisTemplate( JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }

and my repositoy :

    @Repository
    public class PaymentDetailRepoImpl implements PaymentDetailRepo {


        @Autowired RedisTemplate<String, String> redisTemplate;

        private ValueOperations<String, String> operation;

    @PostConstruct
    private void init() {
        operation = redisTemplate.opsForValue();
    }

        @Override
            public void saveMulti(List<Payment> listModel) {
                this.redisTemplate.executePipelined(new RedisCallback<Object>() {
                    @Override
                    public Object doInRedis(RedisConnection connection) throws DataAccessException {
                        StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
                        listModel.forEach(model -> {
                            operation.set(model.getUser(), model.getTotal());
                        });
                        return null;
                    }

                });
            }

the data was successfully import into redis but at the end of transaction i got following error :

org.springframework.data.redis.RedisSystemException: Unknown redis exception; nested exception is java.lang.NullPointerException

cause by:

by: java.lang.NullPointerException: null
    at redis.clients.jedis.Transaction.exec(Transaction.java:54)
    at org.springframework.data.redis.connection.jedis.JedisConnection.exec(JedisConnection.java:558)

do you have any idea why this error occured?

Faced similar error, setting afterPropertiesSet resolved the issue. see https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/beans/factory/InitializingBean.html?is-external=true#afterPropertiesSet-- for more information on afterPropertiesSet.

public RedisTemplate<String, Object> redisTemplate() {
    final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
    template.afterPropertiesSet();
    return template;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM