すべてのプロダクト
Search
ドキュメントセンター

Tair (Redis® OSS-Compatible):トランザクションの処理

最終更新日:Jun 05, 2026

Tair (Redis OSS-compatible) は Redis トランザクションをサポートします。MULTIEXECDISCARDWATCHUNWATCH コマンドを使用して、アトミック操作を実行します。

Redis のトランザクションは、リレーショナルデータベースのトランザクションとは異なります。操作が失敗した場合、または DISCARD を実行した場合でも、Redis はトランザクションをロールバックしません。

コード例

例 1: 2 つのクライアントが異なるキーを操作

サンプルコード:

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";
    // 注意:2つのキーの内容は異なります。
    static String client1_key = "KVStore-Transcation-1";
    static String client2_key = "KVStore-Transcation-2";
    public static void main(String[] args) {
        Jedis jedis = new Jedis(host, port);
        String authString = jedis.auth(password); // インスタンスパスワード (password)
        if (!authString.equals("OK")) {
            System.err.println("Authentication failed: " + authString);
            jedis.close();
            return;
        }
        jedis.set(client1_key, "0");
        // 別のスレッドを開始して、別のクライアントをシミュレートします。
        new KVStoreTranscationTest().new OtherKVStoreClient().start();
        Thread.sleep(500);
        Transaction tx = jedis.multi(); // トランザクションを開始します。
        // 以下の操作をアトミック操作としてサーバーに送信します。
        tx.incr(client1_key);
        tx.incr(client1_key);
        Thread.sleep(400); // このスレッドを一時停止しても、トランザクションのキューイングには影響しません。
        tx.incr(client1_key);
        Thread.sleep(300); // このスレッドを一時停止しても、トランザクションのキューイングには影響しません。
        tx.incr(client1_key);
        Thread.sleep(200); // このスレッドを一時停止しても、トランザクションのキューイングには影響しません。
        tx.incr(client1_key);
        List<Object> result = tx.exec(); // トランザクションを実行します。
        // 結果を解析して出力します。
        for(Object rt : result){
            System.out.println("Client 1 > In transaction > "+rt.toString());
        }
        jedis.close();
    }
    class OtherKVStoreClient extends Thread{
        @Override
        public void run() {
            Jedis jedis = new Jedis(host, port);
            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 < 10; i++) {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Client 2 > "+jedis.incr(client2_key));
            }
            jedis.close();
        }
    }
}

Tair (Redis OSS-compatible) インスタンスの正しいエンドポイントとパスワードを使用してプログラムを実行します。出力では、client1 のトランザクション操作がアトミックブロックとして実行されます。

期待される出力:

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

例 2: 2 つのクライアントが同じキーを操作

この例では、例 1 を変更して、両方のクライアントが同じキーを操作するようにします。残りのコードは変更されません。

    ... ... 
// 注意:2つのキーの内容は同じです。
    static String client1_key = "KVStore-Transcation-1";
    static String client2_key = "KVStore-Transcation-1";
    ... ...

変更したプログラムを実行します。client1 が共有キーでトランザクションを使用していても、client2 はブロックされません。

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