Scenarios

ApsaraDB for Redis supports a feature to define transactions, as in Redis. This allows you to use the  MULTI, EXEC, DISCARD, WATCH, and UNWATCH commands to run atomic transactions.

Note that the definition of transaction in Redis is slightly different from that in relational databases. When an operation in a redis transaction fails, or discards If an operation fails or the transaction is cancelled by the DISCARD command, Redis does not perform transaction rollback.

Sample code 1: Two clients operate on different keys

package transcation.kvstore.aliyun.com;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class KVStoreTranscationTest {
    static final String host = "xxxxxx.m.cnhza.kvstore.aliyuncs.com";
    static final int port = 6379;
    static final String password = "password";
    //**注意这两个key的内容是不同的
    static String client1_key = "KVStore-Transcation-1";
    Static string client2_key = "maid ";
    public static void main(String[] args) {
        Jedis jedis = new Jedis(host, port);
        // ApsaraDB for Redis的实例密码
        String authString = jedis.auth(password);//password
        if (! Authstring. Equals ("OK ")){
            System. Err. println ("authentication failed:" + authstring );
            jedis.close();
            return;
        
        jedis.set(client1_key, "0");
        //启动另一个thread,模拟另外的client
        new KVStoreTranscationTest().new OtherKVStoreClient().start();
        Thread.sleep(500);
        Transaction tx = jedis.multi();//开始事务
        //以下操作会集中提交服务器端处理,作为“原子操作”
        tx.incr(client1_key);
        tx.incr(client1_key);
        Thread.sleep(400);//此处Thread的暂停对事务中前后连续的操作并无影响,其他Thread的操作也无法执行
        tx.incr(client1_key);
        Thread.sleep(300);//此处Thread的暂停对事务中前后连续的操作并无影响,其他Thread的操作也无法执行
        tx.incr(client1_key);
        Thread.sleep(200);//此处Thread的暂停对事务中前后连续的操作并无影响,其他Thread的操作也无法执行
        tx.incr(client1_key);
        List<Object> result = tx.exec();//提交执行
        //解析并打印出结果
        for(Object rt : result){
            System.out.println("Client 1 > 事务中> "+rt.toString());
        
        jedis.close();
    
    class OtherKVStoreClient extends Thread{
        @Override
        public void run() {
            Jedis jedis = new Jedis(host, port);
            // ApsaraDB for Redis的实例密码
            String authstring = Jedis. Auth (password); // Password
            if (! authString.equals("OK")) {
                System.err.println("AUTH Failed: " + authString);
                jedis.close();
                return;
            
            jedis.set(client2_key, "100");
            for (int i = 0; i < n; i++) {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                
                System.out.println("Client 2 > "+jedis.incr(client2_key));
            
            jedis.close();
        
    

Output 1

After you access the ApsaraDB for Redis instance with the correct address and password and run the preceding Java code, the following output is displayed.  Here, we can see that Client 1 and Client 2 are in different threads.  The operations in the transaction submitted by Client 1 are run sequentially. Client 2 requests for operating on another key during this period, but the operation is blocked and Client 2 has to wait  until all the operations in Client 1’s transaction are completed.

Client 2> 101
Client 2> 102
Client 2> 103
Client 2> 104
Client 1 > Transaction > 1
Client 1> Transaction> 2
Client 1> Transaction> 3
Client 1> Transaction> 4
Client 1> Transaction> 5
Client 2> 105
Client 2> 106
Client 2> 107
Client 2> 108
Client 2> 109
Client 2> 110

Sample code 2: Two clients operate on the same key

By slightly modifying the preceding code, we can have the two clients operate on the same key. The other parts of the code remain unchanged.

     
// *** Note that the contents of these two keys are now the same
    static String client1_key = "KVStore-Transcation-1";
    static String client2_key = "KVStore-Transcation-1";
    

Output 2

After running the modified Java code, the output is displayed as follows. We can see that the two clients are in different threads but operate on the same key. However, while Client 1 uses the transaction feature to operate on this key, Client 2 is blocked and has to wait until all the operations in Client 1’s transaction are completed.

Client 2> 101
Client 2> 102
Client 2> 103
Client 2> 104
Client 1 > Transaction > 105
Client 1> Transaction> 106
Client 1> Transaction> 107
Client 1> Transaction> 108
Client 1> Transaction> 109
Client 2> 110
Client 2> 111
Client 2> 112
Client 2> 113
Client 2> 114
Client 2> 115