Interface TransactionsClusterCommands
-
- All Known Implementing Classes:
GlideClusterClient
public interface TransactionsClusterCommandsSupports commands for the "Transactions Commands" group for cluster clients.- See Also:
- Transactions Commands
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description java.util.concurrent.CompletableFuture<java.lang.Object[]>exec(ClusterBatch batch, boolean raiseOnError)Executes a batch by processing the queued commands.java.util.concurrent.CompletableFuture<java.lang.Object[]>exec(ClusterBatch batch, boolean raiseOnError, ClusterBatchOptions options)Executes a batch by processing the queued commands.java.util.concurrent.CompletableFuture<java.lang.Object[]>exec(ClusterTransaction transaction)Deprecated.Useexec(ClusterBatch, boolean)instead.java.util.concurrent.CompletableFuture<java.lang.Object[]>exec(ClusterTransaction transaction, RequestRoutingConfiguration.SingleNodeRoute route)Deprecated.Useexec(ClusterBatch, boolean, ClusterBatchOptions)instead.java.util.concurrent.CompletableFuture<java.lang.String>unwatch()Flushes all the previously watched keys for a transaction.java.util.concurrent.CompletableFuture<java.lang.String>unwatch(RequestRoutingConfiguration.Route route)Flushes all the previously watched keys for a transaction.
-
-
-
Method Detail
-
exec
@Deprecated java.util.concurrent.CompletableFuture<java.lang.Object[]> exec(ClusterTransaction transaction)
Deprecated.Useexec(ClusterBatch, boolean)instead. This method is being replaced by a more flexible approach usingClusterBatch.Executes a transaction by processing the queued commands.
The transaction will be routed to the slot owner of the first key found in the transaction. If no key is found, the command will be sent to a random node.
- Parameters:
transaction- AClusterTransactionobject containing a list of commands to be executed.- Returns:
- A list of results corresponding to the execution of each command in the transaction.
- See Also:
exec(ClusterBatch, boolean), valkey.io documentation on Transactions- Example:
ClusterTransaction transaction = new ClusterTransaction() .customCommand(new String[] {"info"}); Object[] result = clusterClient.exec(transaction).get(); assert ((String) result[0]).contains("# Stats");
-
exec
@Deprecated java.util.concurrent.CompletableFuture<java.lang.Object[]> exec(ClusterTransaction transaction, RequestRoutingConfiguration.SingleNodeRoute route)
Deprecated.Useexec(ClusterBatch, boolean, ClusterBatchOptions)instead. This method is being replaced by a more flexible approach usingClusterBatchandClusterBatchOptions.Executes a transaction by processing the queued commands.
- Parameters:
transaction- AClusterTransactionobject containing a list of commands to be executed.route- A single-node routing configuration for the transaction. The client will route the transaction to the node defined byroute.- Returns:
- A list of results corresponding to the execution of each command in the transaction.
- See Also:
exec(ClusterBatch, boolean, ClusterBatchOptions), valkey.io documentation on Transactions- Example:
ClusterTransaction transaction = new ClusterTransaction().ping().info(); Object[] result = clusterClient.exec(transaction, RANDOM).get(); assert ((String) result[0]).equals("PONG"); assert ((String) result[1]).contains("# Stats");
-
exec
java.util.concurrent.CompletableFuture<java.lang.Object[]> exec(ClusterBatch batch, boolean raiseOnError)
Executes a batch by processing the queued commands.Routing Behavior:
- For atomic batches (Transactions):
- The transaction will be routed to the slot owner of the first key found in the batch.
- If no key is found, the request will be sent to a random node.
- For non-atomic batches:
- Each command will be routed to the node that owns the corresponding key's slot. If no key is present, the routing will follow the default policy for the command.
- Multi-node commands will be automatically split and sent to the respective nodes.
Notes:
- Atomic Batches (Transactions): All key-based commands must map to the
same hash slot. If keys span different slots, the transaction will fail. If the
transaction fails due to a
WATCHcommand,EXECwill returnnull.
- Parameters:
batch- AClusterBatchobject containing a list of commands to be executed.raiseOnError- Determines how errors are handled within the batch response.When set to
true, the first encountered error in the batch will be raised as an exception of typeRequestExceptionafter all retries and reconnections have been executed.When set to
false, errors will be included as part of the batch response, allowing the caller to process both successful and failed commands together. In this case, error details will be provided as instances ofRequestException.- Returns:
- A
CompletableFutureresolving to an array of results, where each entry corresponds to a command’s execution result. - See Also:
- Valkey Transactions (Atomic Batches), Valkey Pipelines (Non-Atomic Batches)
- Example:
// Example 1: Atomic Batch (Transaction) ClusterBatch atomicBatch = new ClusterBatch(true) // Atomic (Transaction) .set("key", "1") .incr("key") .get("key"); Object[] atomicResult = clusterClient.exec(atomicBatch, true).get(); System.out.println("Atomic Batch Result: " + Arrays.toString(atomicResult)); // Expected Output: Atomic Batch Result: [OK, 2, 2] // Example 2: Non-Atomic Batch (Pipeline) ClusterBatch nonAtomicBatch = new ClusterBatch(false) // Non-Atomic (Pipeline) .set("key1", "value1") .set("key2", "value2") .get("key1") .get("key2"); Object[] nonAtomicResult = clusterClient.exec(nonAtomicBatch, true).get(); System.out.println("Non-Atomic Batch Result: " + Arrays.toString(nonAtomicResult)); // Expected Output: Non-Atomic Batch Result: [OK, OK, value1, value2]
- For atomic batches (Transactions):
-
exec
java.util.concurrent.CompletableFuture<java.lang.Object[]> exec(ClusterBatch batch, boolean raiseOnError, ClusterBatchOptions options)
Executes a batch by processing the queued commands.Routing Behavior:
- If a
routeis specified inClusterBatchOptions, the entire batch is sent to the specified node. - If no
routeis specified:- Atomic batches (Transactions): Routed to the slot owner of the first key in the batch. If no key is found, the request is sent to a random node.
- Non-atomic batches (Pipelines): Each command is routed to the node owning the corresponding key's slot. If no key is present, routing follows the command's request policy. Multi-node commands are automatically split and dispatched to the appropriate nodes.
Behavior notes:
- Atomic Batches (Transactions): All key-based commands must map to the
same hash slot. If keys span different slots, the transaction will fail. If the
transaction fails due to a
WATCHcommand,EXECwill returnnull.
Retry and Redirection:
- If a redirection error occurs:
- Atomic batches (Transactions): The entire transaction will be redirected.
- Non-atomic batches: Only commands that encountered redirection errors will be redirected.
- Retries for failures will be handled according to the configured
ClusterBatchRetryStrategy.
- Parameters:
batch- AClusterBatchcontaining the commands to execute.raiseOnError- Determines how errors are handled within the batch response.When set to
true, the first encountered error in the batch will be raised as an exception of typeRequestExceptionafter all retries and reconnections have been executed.When set to
false, errors will be included as part of the batch response, allowing the caller to process both successful and failed commands together. In this case, error details will be provided as instances ofRequestException.options- AClusterBatchOptionsobject containing execution options.- Returns:
- A
CompletableFutureresolving to an array of results, where each entry corresponds to a command’s execution result. - See Also:
- Valkey Transactions (Atomic Batches), Valkey Pipelines (Non-Atomic Batches)
- Example:
// Atomic batch (transaction): all keys must share the same hash slot ClusterBatchOptions options = ClusterBatchOptions.builder() .timeout(1000) // Set a timeout of 1000 milliseconds .build(); ClusterBatch atomicBatch = new ClusterBatch(true) .set("key", "1") .incr("key") .get("key"); Object[] atomicResult = clusterClient.exec(atomicBatch, false, options).get(); System.out.println("Atomic Batch Result: " + Arrays.toString(atomicResult)); // Output: [OK, 2, 2] // Non-atomic batch (pipeline): keys may span different hash slots ClusterBatchOptions pipelineOptions = ClusterBatchOptions.builder() .retryStrategy(ClusterBatchRetryStrategy.builder() .retryServerError(true) .retryConnectionError(false) .build()) .build(); ClusterBatch nonAtomicBatch = new ClusterBatch(false) .set("key1", "value1") .set("key2", "value2") .get("key1") .get("key2"); Object[] nonAtomicResult = clusterClient.exec(nonAtomicBatch, false, pipelineOptions).get(); System.out.println("Non-Atomic Batch Result: " + Arrays.toString(nonAtomicResult)); // Output: [OK, OK, value1, value2]
- If a
-
unwatch
java.util.concurrent.CompletableFuture<java.lang.String> unwatch()
Flushes all the previously watched keys for a transaction. Executing a transaction will automatically flush all previously watched keys.
The command will be routed to all primary nodes.- Returns:
OK.- See Also:
- valkey.io for details.
- Example:
assert client.watch(new String[] {"sampleKey"}).get().equals("OK"); assert client.unwatch().get().equals("OK"); // Flushes "sampleKey" from watched keys.
-
unwatch
java.util.concurrent.CompletableFuture<java.lang.String> unwatch(RequestRoutingConfiguration.Route route)
Flushes all the previously watched keys for a transaction. Executing a transaction will automatically flush all previously watched keys.- Parameters:
route- Specifies the routing configuration for the command. The client will route the command to the nodes defined byroute.- Returns:
OK.- See Also:
- valkey.io for details.
- Example:
assert client.watch(new String[] {"sampleKey"}).get().equals("OK"); assert client.unwatch(ALL_PRIMARIES).get().equals("OK"); // Flushes "sampleKey" from watched keys for all primary nodes.
-
-