Interface TransactionsCommands

  • All Known Implementing Classes:
    GlideClient

    public interface TransactionsCommands
    Supports commands for the "Transactions Commands" group for standalone 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​(Batch batch, boolean raiseOnError)
      Executes a batch by processing the queued commands.
      java.util.concurrent.CompletableFuture<java.lang.Object[]> exec​(Batch batch, boolean raiseOnError, BatchOptions options)
      Executes a batch by processing the queued commands with additional options.
      java.util.concurrent.CompletableFuture<java.lang.Object[]> exec​(Transaction transaction)
      Deprecated.
      java.util.concurrent.CompletableFuture<java.lang.String> unwatch()
      Flushes all the previously watched keys for a transaction.
    • Method Detail

      • exec

        @Deprecated
        java.util.concurrent.CompletableFuture<java.lang.Object[]> exec​(Transaction transaction)
        Deprecated.
        Use exec(Batch, boolean) instead. This method is being replaced by a more flexible approach using Batch.

        Executes a transaction by processing the queued commands.

        Parameters:
        transaction - A Transaction object 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(Batch, boolean), valkey.io for details on Transactions.
        Example:
        
         Transaction transaction = new Transaction().customCommand(new String[] {"info"});
         Object[] result = client.exec(transaction).get();
         assert ((String) result[0]).contains("# Stats");
         
      • exec

        java.util.concurrent.CompletableFuture<java.lang.Object[]> exec​(Batch batch,
                                                                        boolean raiseOnError)
        Executes a batch by processing the queued commands.

        Notes:

        • Atomic Batches - Transactions: If the transaction fails due to a WATCH command, EXEC will return null.
        Parameters:
        batch - A Batch containing 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 type RequestException after 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 of RequestException.

        Returns:
        A CompletableFuture resolving 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)
         Batch transaction = new Batch(true) // Atomic (Transactional)
             .set("key", "1")                 // Set a value for key
             .incr("key")                     // Increment the value of the key
             .get("key");                     // Get the value of the key
         Object[] result = client.exec(transaction, true).get();
         System.out.println("Transaction Batch Result: " + Arrays.toString(result));
         // Expected Output: Transaction Batch Result: [OK, 2, 2]
        
         // Example 2: Non-Atomic Batch (Pipeline)
         Batch pipeline = new Batch(false) // Non-Atomic (Pipeline)
             .set("key1", "value1")          // Set value for key1
             .set("key2", "value2")          // Set value for key2
             .get("key1")                    // Get value for key1
             .get("key2");                   // Get value for key2
         Object[] pipelineResult = client.exec(pipeline, true).get();
         System.out.println("Pipeline Batch Result: " + Arrays.toString(pipelineResult));
         // Expected Output: Pipeline Batch Result: [OK, OK, value1, value2]
         
      • exec

        java.util.concurrent.CompletableFuture<java.lang.Object[]> exec​(Batch batch,
                                                                        boolean raiseOnError,
                                                                        BatchOptions options)
        Executes a batch by processing the queued commands with additional options.

        Notes:

        • Atomic Batches - Transactions: If the transaction fails due to a WATCH command, EXEC will return null.
        Parameters:
        batch - A Batch containing 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 type RequestException after 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 of RequestException.

        options - A BatchOptions object containing execution options.
        Returns:
        A CompletableFuture resolving 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) with BatchOptions
          BatchOptions options = BatchOptions.builder()
             .timeout(1000) // Set a timeout of 1000 milliseconds
             .build();
        
         Batch transaction = new Batch(true) // Atomic (Transactional)
             .set("key", "1")
             .incr("key")
             .customCommand(new String[] {"get", "key"});
         Object[] result = client.exec(transaction, false, options).get();
         System.out.println("Transaction Result: " + Arrays.toString(result));
         // Expected Output: Transaction Result: [OK, 2, 2]
        
         // Example 2: Non-Atomic Batch (Pipeline) with BatchOptions
         // Commands can operate on different hash slots.
         BatchOptions options = BatchOptions.builder()
             .timeout(1000) // Set a timeout of 1000 milliseconds
             .build();
        
         Batch pipeline = new Batch(false) // Non-Atomic (Pipeline)
             .customCommand(new String[] {"set", "key1", "value1"})
             .customCommand(new String[] {"set", "key2", "value2"})
             .customCommand(new String[] {"get", "key1"})
             .customCommand(new String[] {"get", "key2"});
         Object[] result = client.exec(pipeline, false, options).get();
         System.out.println("Pipeline Result: " + Arrays.toString(result));
         // Expected Output: Pipeline Result: [OK, OK, value1, value2]
         
      • 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.
        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.