-
Couldn't load subscription status.
- Fork 1.2k
Description
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
RedisTemplateand 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
@Transactionalmethod. - 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)