preface:

Transactions in Redis allow a set of commands to be executed in a single step. Managing Redis transactions in Java can be tricky for some users, but it’s easier if you have the right tools. This article covers everything you need to know about executing Redis transactions on Java, as well as a brief introduction to Spring transaction manager and Redis XA transactions.

Redis transactions on Java

Redis transactions are atomic, which means that all or none of the commands in the transaction are processed. These commands are executed as a single sequenced operation, without selecting requests made by another client during the execution of the transaction.

Redis transactions are based on four commands: MULTI, EXEC, DISCARD, and WATCH. The MULTI command allows the user to issue multiple commands, all of which are executed when the user calls EXEC. Even if commands in multiple command lists fail, all other commands in the queue are processed. If this happens, the user will see an error message.

Redisson is a Redis Java client that allows us to perform READ_COMMITTED transactions on Java at the isolation level. Some examples of participating Java transaction objects are RMap, RMapCache, LocalCachedMap, RSET, RSetCache, and RBucket.

The supported Redis modes are:

  • SINGLE

  • MASTER/SLAVE

  • SENTINEL

  • ELASTICACHE REPLICATED

  • AZURE CACHE

  • RLEC

There are several options available when creating a transaction. These include:

TransactionOptions options = TransactionOptions.defaults()
// The data synchronization between the Redis master server and its slave server in the transaction timed out.
// Default is 5000 ms
options.syncSlavesTimeout(5, TimeUnit.SECONDS);
// Response timed out
// The default is 3000 milliseconds
options.responseTimeout(3, TimeUnit.SECONDS);
// Define the time interval for each attempt to send a transaction (if not already sent).
// The default is 1500 milliseconds
options.retryInterval(2, TimeUnit.SECONDS)

// Define the number of attempts to send the transaction if it has not been sent yet.
// The default is 3 attempts
options.retryAttempts(3)
// If the transaction has not committed within the timeout, it is automatically rolled back.
// Default is 5000 ms
options.timeout(5, TimeUnit.SECONDS);Copy the code

Redis Spring transaction manager

Redisson by with Spring org. Springframework. Transaction. The PlatformTransactionManager.

1. Configuration RedissonTransactionContextConfig

@Configuration
@EnableTransactionManagementpublic 
public class RedissonTransactionContextConfig {
    @Bean 
    public TransactionalBean transactionBean(a) {
        return new TransactionalBean(); 
    } 
    @Bean public RedissonTransactionManager transactionManager(RedissonClient redisson) { 
        return new RedissonTransactionManager(redisson); 
    } 
    @Bean 
    public RedissonClient redisson(a) { 
        return BaseTest.createInstance(); 
    }
    @PreDestroy
    public void destroy(a) { redisson().shutdown(); }}public class TransactionalBean { 
    @Autowired 
    private RedissonTransactionManager transactionManager; 
    @Transactional 
    public void commitData(a) { 
        RTransaction transaction = transactionManager.getCurrentTransaction(); 
        RMap map = transaction.getMap("test1"); map.put("1"."2"); }}Copy the code

You can then use Spring Transaction Manager to manage transactions in Redis.

XA transactions in Redis

Redisson also provides the XAResource implementation. This allows the participation of JTA transactions to perform distributed transaction processing.

XA is a bidirectional system interface. Distributed transactions are formed by an Application Program, a Transaction Manager, and one or more Resource Managers to form a communication bridge. The transaction manager controls JTA transactions, manages the transaction life cycle, and coordinates resources.

In the JTA transaction manager for abstract javax.mail. Transaction. The TransactionManager interface, and through the underlying transaction service (i.e., JTS) implementation. The resource manager is responsible for controlling and managing real resources (such as databases or JMS queues).

Here is an example of Redisson executing XA Transactions:

// Transactions obtained from the JTA compatible transaction manager
Transaction globalTransaction = transactionManager.getTransaction();
RXAResource xaResource = redisson.getXAResource();
globalTransaction.enlistResource(xaResource);
RTransaction transaction = xaResource.getTransaction();
RBucket<String> bucket = transaction.getBucket("myBucket");
bucket.set("simple");
RMap<String, String> map = transaction.getMap("myMap");
map.put("myKey"."myValue");
transactionManager.commit();Copy the code

See more uses of Redission:

  • Blog.csdn.net/u011663149/…
  • Blog.csdn.net/u011663149/…
  • Blog.csdn.net/u011663149/…