Skip to content

Commit

Permalink
Improved Transaction classes documentation (#3262)
Browse files Browse the repository at this point in the history
* More info in docs of transaction classes

* Apply code reviews

* More docs

* Code comment
  • Loading branch information
sazzad16 authored Jan 2, 2023
1 parent 4dad4c8 commit 82f286b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/main/java/redis/clients/jedis/ReliableTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,32 @@
import java.util.List;
import redis.clients.jedis.exceptions.JedisException;

/**
* ReliableTransaction is a transaction where commands are immediately sent to Redis server and the
* 'QUEUED' reply checked.
*/
public class ReliableTransaction extends TransactionBase {

private static final String QUEUED_STR = "QUEUED";

/**
* Creates a new transaction.
*
* A MULTI command will be added to be sent to server. WATCH/UNWATCH/MULTI commands must not be
* called with this object.
*/
public ReliableTransaction(Connection connection) {
super(connection);
}

/**
* If you want to WATCH/UNWATCH keys before MULTI command you should do {@code doMulti = true}.
* Creates a new transaction.
*
* A user wanting to WATCH/UNWATCH keys followed by a call to MULTI ({@link #multi()}) it should
* be {@code doMulti=false}.
*
* @param connection connection
* @param doMulti {@code false} should be set to enable manual WATCH, UNWATCH and MULTI
*/
public ReliableTransaction(Connection connection, boolean doMulti) {
super(connection, doMulti);
Expand All @@ -27,7 +45,7 @@ protected final void processMultiResponse() {
@Override
protected final void processAppendStatus() {
String status = connection.getStatusCodeReply();
if (!"QUEUED".equals(status)) {
if (!QUEUED_STR.equals(status)) {
throw new JedisException(status);
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/redis/clients/jedis/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@

import java.util.List;

/**
* A pipeline based transaction.
*/
public class Transaction extends TransactionBase {

private final Jedis jedis;

// Legacy - to support Jedis.multi()
// TODO: Should be package private ??
public Transaction(Jedis jedis) {
super(jedis.getConnection());
this.jedis = jedis;
}

/**
* Creates a new transaction.
*
* A MULTI command will be added to be sent to server. WATCH/UNWATCH/MULTI commands must not be
* called with this object.
*/
public Transaction(Connection connection) {
super(connection);
this.jedis = null;
}

/**
* If you want to WATCH/UNWATCH keys before MULTI command you should do {@code doMulti = true}.
* Creates a new transaction.
*
* A user wanting to WATCH/UNWATCH keys followed by a call to MULTI ({@link #multi()}) it should
* be {@code doMulti=false}.
*
* @param connection connection
* @param doMulti {@code false} should be set to enable manual WATCH, UNWATCH and MULTI
*/
public Transaction(Connection connection, boolean doMulti) {
super(connection, doMulti);
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/redis/clients/jedis/TransactionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,24 @@ public abstract class TransactionBase extends Queable implements PipelineCommand
private boolean inWatch = false;
private boolean inMulti = false;

/**
* Creates a new transaction.
*
* A MULTI command will be added to be sent to server. WATCH/UNWATCH/MULTI commands must not be
* called with this object.
*/
public TransactionBase(Connection connection) {
this(connection, true);
}

/**
* If you want to WATCH/UNWATCH keys before MULTI command you should do {@code doMulti = true}.
* Creates a new transaction.
*
* A user wanting to WATCH/UNWATCH keys followed by a call to MULTI ({@link #multi()}) it should
* be {@code doMulti=false}.
*
* @param connection connection
* @param doMulti {@code false} should be set to enable manual WATCH, UNWATCH and MULTI
*/
public TransactionBase(Connection connection, boolean doMulti) {
this.connection = connection;
Expand Down

0 comments on commit 82f286b

Please sign in to comment.