DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build Self-Hosted RSS Feed Reader Using Spring Boot and Redis
  • How to Connect Redis Sentinel With Spring
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • When Kubernetes Breaks Session Consistency: Using Cosmos DB and Redis Together

Trending

  • DevOps Is Dead, Long Live Platform Engineering
  • How to Test a PATCH API Request With REST-Assured Java
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  1. DZone
  2. Coding
  3. Frameworks
  4. Reactive Spring Transactions in Redis

Reactive Spring Transactions in Redis

Learn how Redisson supports reactive Spring transactions.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Updated Jan. 27, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.0K Views

Join the DZone community and get the full member experience.

Join For Free

Speaking of transactions...


Spring is an open-source, lightweight Java application framework that simplifies the process of developing enterprise software for the Java programming language. The Spring framework includes many core features that promote good software development practices, such as dependency injection and a model-view-controller (MVC) architectural pattern.

Reactive programming is a declarative programming paradigm that works with asynchronous data streams in order to respond to real-time events and build scalable, resilient, concurrent applications. Beginning with Spring Framework 5.2 M2, Spring includes support for reactive transaction management.

Spring includes a Spring Data Redis module for compatibility with Redis, an in-memory data structure store often used to build NoSQL databases. However, Spring doesn't provide support for Redis-based transactions out of the box. For this reason, many Java developers use a third-party Java Redis framework like Redisson to perform reactive Spring transactions in Redis.

Redisson: Reactive Spring Transactions in Redis

Redisson is a third-party open-source Redis Java client. By including distributed implementations of many common Java objects and collections, Redisson dramatically simplifies the process of using Java with Redis.

Redisson offers support for reactive Spring transactions in Redis via interfaces such as RedissonReactiveClient, RTransactionReactive, and ReactiveRedissonTransactionManager. To understand how these all work together, below is an example of how to do reactive Spring transactions in Redis using Redisson:

Java
 




xxxxxxxxxx
1
38


 
1
public class TransactionalBean {
2

          
3
    @Autowired
4
    private ReactiveRedissonTransactionManager transactionManager;
5

          
6
    @Transactional
7
    public Mono<Void> commitData() {
8
        Mono<RTransactionReactive> transaction = transactionManager.getCurrentTransaction();
9
        return transaction.flatMap(t -> {
10
            RMapReactive<String, String> map = t.getMap("myMap");
11
            return map.put("1", "2");
12
        }).then();
13
    }
14

          
15
}
16

          
17

          
18
@Configuration
19
@EnableTransactionManagement
20
public class RedissonReactiveTransactionContextConfig {
21
     
22
     @Bean
23
     public TransactionalBean transactionBean() {
24
         return new TransactionalBean();
25
     }
26
     
27
     @Bean
28
     public ReactiveRedissonTransactionManager transactionManager(RedissonReactiveClient redisson) {
29
         return new ReactiveRedissonTransactionManager(redisson);
30
     }
31
     
32
     @Bean(destroyMethod="shutdown")
33
     public RedissonReactiveClient redisson(@Value("classpath:/redisson.yaml") Resource configFile) throws IOException {
34
          Config config = Config.fromYAML(configFile.getInputStream());
35
         return Redisson.createReactive(config);
36
     }
37
     
38
}



Reactive Spring transactions in Redis are just one example of how developers can implement reactive programming, Java transactions, and the Spring framework using Redisson. The Spring framework is fully compatible with Redis, thanks to the Redisson Java client for Redis.

To get started using Spring with Redisson, check out the Configuration section in the Redisson project wiki. For more information about reactive programming and reactive Spring transactions in Redis, check out the Redisson website.

Further Reading

Using Spring Data Redis in Spring Boot 2.0 to Write a Custom Query

Implementation of Redis in a Microservice/Spring Boot Application

Spring Framework Redis (company)

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Self-Hosted RSS Feed Reader Using Spring Boot and Redis
  • How to Connect Redis Sentinel With Spring
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • When Kubernetes Breaks Session Consistency: Using Cosmos DB and Redis Together

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook