NullPointerException when using @Transactional with Spring Data Redis

22 Views Asked by At

I'm encountering a NullPointerException when trying to use the @Transactional annotation with Spring Data Redis in my Spring Boot application. I have a method annotated with @Transactional where I'm performing operations on Redis using calling save method repository. However, when I execute the method, I receive a NullPointerException. Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "org.springframework.data.redis.connection.RedisConnection.del(byte[][])" is null at org.springframework.data.redis.core.RedisKeyValueAdapter.lambda$put$0(RedisKeyValueAdapter.java:217) ~[spring-data-redis-3.2.3.jar:3.2.3] at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:396) ~[spring-data-redis-3.2.3.jar:3.2.3] at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:363) ~[spring-data-redis-3.2.3.jar:3.2.3] at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:350) ~[spring-data-redis-3.2.3.jar:3.2.3] at org.springframework.data.redis.core.RedisKeyValueAdapter.put(RedisKeyValueAdapter.java:212) ~[spring-data-redis-3.2.3.jar:3.2.3] at org.springframework.data.keyvalue.core.KeyValueTemplate.lambda$update$1(KeyValueTemplate.java:201) ~[spring-data-keyvalue-3.2.3.jar:3.2.3] at org.springframework.data.keyvalue.core.KeyValueTemplate.execute(KeyValueTemplate.java:314) ~[spring-data-keyvalue-3.2.3.jar:3.2.3] ... 73 common frames omitted

I'm using Spring Boot 3.2.3 and Spring Data Redis 3.2.3. I have configured my RedisTemplate with transaction support enabled, and I'm using the Lettuce client for Redis connectivity.

Any insights into why this NullPointerException might be occurring and how I can resolve it would be greatly appreciated.

I'm encountering a NullPointerException when trying to use the @Transactional annotation with Spring Data Redis in my Spring Boot application. Here's a breakdown of what I've tried and the behavior I was expecting:

I've annotated my service method with @Transactional to ensure that the Redis operations are executed within a transactional context. I've configured my RedisTemplate with transaction support enabled by setting template.setEnableTransactionSupport(true). I configured also a datasource and PlatformTransactionManager


    @Bean
    public DataSource dataSource() throws SQLException {
        EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder();
        databaseBuilder.setName("transactionDatabase");
        databaseBuilder.setType(EmbeddedDatabaseType.H2);
        return databaseBuilder.build();
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException {
        return new DataSourceTransactionManager(dataSource());
    }
0

There are 0 best solutions below