場景介紹
雲數據庫 Redis 版支持 Redis 中 定義的“事務(transaction)”機制,即用戶可以使用 MULTI,EXEC,DISCARD,WATCH,UNWATCH 指令用來執行原子性的事務操作。
需要強調的是,Redis 中定義的事務,並不是關係數據庫中嚴格意義上的事務。當 Redis 事務中的某個操作執行失敗,或者用 DISCARD 取消事務時候,Redis 並不執行“事務回滾”,在使用時要注意這點。
代碼示例1:兩個 client 操作不同的 key
package transcation.kvstore.aliyun.com;
- 1
- 1
import java.util.List;
- 1
- 1
import redis.clients.jedis.Jedis;
- 1
import redis.clients.jedis.Transaction;
- 1
- 1
public class KVStoreTranscationTest {
- 1
- 1
static final String host = "xxxxxx.m.cnhza.kvstore.aliyuncs.com";
- 1
static final int port = 6379;
- 1
static final String password = "password";
- 1
- 1
//**注意這兩個key的內容是不同的
- 1
static String client1_key = "KVStore-Transcation-1";
- 1
static String client2_key = "KVStore-Transcation-2";
- 1
- 1
public static void main(String[] args) {
- 1
- 1
Jedis jedis = new Jedis(host, port);
- 1
- 1
// ApsaraDB for Redis的實例密碼
- 1
String authString = jedis.auth(password);//password
- 1
- 1
if (!authString.equals("OK")) {
- 1
System.err.println("認證失敗: " + authString);
- 1
jedis.close();
- 1
return;
- 1
}
- 1
- 1
jedis.set(client1_key, "0");
- 1
- 1
//啓動另一個thread,模擬另外的client
- 1
new KVStoreTranscationTest().new OtherKVStoreClient().start();
- 1
- 1
Thread.sleep(500);
- 1
- 1
Transaction tx = jedis.multi();//開始事務
- 1
- 1
//以下操作會集中提交服務器端處理,作爲“原子操作”
- 1
tx.incr(client1_key);
- 1
tx.incr(client1_key);
- 1
Thread.sleep(400);//此處Thread的暫停對事務中前後連續的操作並無影響,其他Thread的操作也無法執行
- 1
tx.incr(client1_key);
- 1
Thread.sleep(300);//此處Thread的暫停對事務中前後連續的操作並無影響,其他Thread的操作也無法執行
- 1
tx.incr(client1_key);
- 1
Thread.sleep(200);//此處Thread的暫停對事務中前後連續的操作並無影響,其他Thread的操作也無法執行
- 1
tx.incr(client1_key);
- 1
- 1
List<Object> result = tx.exec();//提交執行
- 1
- 1
//解析並打印出結果
- 1
for(Object rt : result){
- 1
System.out.println("Client 1 > 事務中> "+rt.toString());
- 1
}
- 1
- 1
jedis.close();
- 1
}
- 1
- 1
class OtherKVStoreClient extends Thread{
- 1
@Override
- 1
public void run() {
- 1
- 1
Jedis jedis = new Jedis(host, port);
- 1
// ApsaraDB for Redis的實例密碼
- 1
String authString = jedis.auth(password);// password
- 1
- 1
if (!authString.equals("OK")) {
- 1
System.err.println("AUTH Failed: " + authString);
- 1
jedis.close();
- 1
return;
- 1
}
- 1
- 1
jedis.set(client2_key, "100");
- 1
- 1
for (int i = 0; i < 10; i++) {
- 1
try {
- 1
Thread.sleep(300);
- 1
} catch (InterruptedException e) {
- 1
e.printStackTrace();
- 1
}
- 1
- 1
System.out.println("Client 2 > "+jedis.incr(client2_key));
- 1
}
- 1
- 1
jedis.close();
- 1
}
- 1
}
- 1
}
- 1
運行結果1
在輸入了正確的雲數據庫 Redis 版實例訪問地址和密碼之後,運行以上 Java 程序,輸出結果如下。從中可以看到 client1 和 client2 在兩個不同的 Thread 中,client1 所提交的事務操作都是集中順序執行的,在此期間儘管 client2 是對另外一個 key 進行操作,它的命令操作也都被阻塞等待,直至 client1 事務中的全部操作執行完畢。
Client 2 > 101
- 1
Client 2 > 102
- 1
Client 2 > 103
- 1
Client 2 > 104
- 1
Client 1 > 事務中> 1
- 1
Client 1 > 事務中> 2
- 1
Client 1 > 事務中> 3
- 1
Client 1 > 事務中> 4
- 1
Client 1 > 事務中> 5
- 1
Client 2 > 105
- 1
Client 2 > 106
- 1
Client 2 > 107
- 1
Client 2 > 108
- 1
Client 2 > 109
- 1
Client 2 > 110
- 1
代碼示例2:兩個 client 操作相同的 key
對以上的代碼稍作改動,使得兩個 client 操作同一個 key,其餘部分保持不變。
... ...
- 1
- 1
//**注意這兩個key的內容現在是相同的
- 1
static String client1_key = "KVStore-Transcation-1";
- 1
static String client2_key = "KVStore-Transcation-1";
- 1
- 1
... ...
- 1
運行結果2
再次運行修改後的此 Java 程序,輸出結果如下。可以看到不同 Thread 中的兩個 client 在操作同一個 key,但是當 client1 利用事務機制來操作這個 key 時,client2 被阻塞不得不等待 client1 事務中的操作完全執行完畢。
Client 2 > 101
- 1
Client 2 > 102
- 1
Client 2 > 103
- 1
Client 2 > 104
- 1
Client 1 > 事務中> 105
- 1
Client 1 > 事務中> 106
- 1
Client 1 > 事務中> 107
- 1
Client 1 > 事務中> 108
- 1
Client 1 > 事務中> 109
- 1
Client 2 > 110
- 1
Client 2 > 111
- 1
Client 2 > 112
- 1
Client 2 > 113
- 1
Client 2 > 114
- 1
Client 2 > 115