spring-projects/spring-data-redis

Feature Request: PlatformTransactionManager for Redis-only Applications

Opened this issue · 1 comments

Description

Currently, Spring Data Redis allows RedisTemplate to participate in transactions by enabling setEnableTransactionSupport(true). However, declarative transaction management (@Transactional) requires a PlatformTransactionManager. The current documentation assumes that applications already have a JDBC DataSourceTransactionManager or similar, which makes Redis transactions only usable in applications that also use a database.

For applications that use Redis exclusively, there is no out-of-the-box PlatformTransactionManager implementation. This limits the ability to use @Transactional or TransactionTemplate with Redis in a consistent Spring transaction management style.

Proposed Solution

Provide a new implementation of PlatformTransactionManager specifically for Redis-only applications. Key features could include:

  • Integration with RedisTemplate and its transaction support (MULTI/EXEC/DISCARD).
  • Thread-bound connection management, similar to current transactional support when setEnableTransactionSupport(true) is enabled.
  • Support for rollback semantics if an exception occurs within a @Transactional method.
  • Minimal or no dependencies on a JDBC DataSource.

Example Usage

@Configuration
@EnableTransactionManagement
public class RedisTxOnlyConfiguration {

    @Bean
    public StringRedisTemplate redisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory());
        template.setEnableTransactionSupport(true);
        return template;
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        // Lettuce or Jedis
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new RedisTransactionManager(redisTemplate());
    }
}

This would allow fully declarative @Transactional support for Redis-only applications, making transaction management consistent across Spring components.

Benefits

  • Unified transaction management for Redis-only applications.
  • Reduces the need for workaround solutions using programmatic transaction handling.
  • Aligns Redis support with Spring’s existing transaction model.

Additional Context

Related documentation: [Spring Data Redis – Transaction Support](https://docs.spring.io/spring-data/redis/docs/current/reference/html/#redis:transactions)

Thanks for bringing this up. We had such an idea a couple years ago but because most applications have been used together with another database and because Redis doesn't have this ACID notion of transactions, we considered the value of a RedisTransactionManager rather low and never introduced a dedicated transaction manager.

It makes sense to have a standalone TransactionManager, also to be consistent across other Spring Data modules.