@@ -40,6 +40,7 @@ public abstract class TransactionBase extends Queable implements PipelineCommand
40
40
PipelineBinaryCommands , RedisModulePipelineCommands , Closeable {
41
41
42
42
protected final Connection connection ;
43
+ private final boolean closeConnection ;
43
44
private final CommandObjects commandObjects ;
44
45
private final GraphCommandObjects graphCommandObjects ;
45
46
@@ -52,6 +53,7 @@ public abstract class TransactionBase extends Queable implements PipelineCommand
52
53
*
53
54
* A MULTI command will be added to be sent to server. WATCH/UNWATCH/MULTI commands must not be
54
55
* called with this object.
56
+ * @param connection connection
55
57
*/
56
58
public TransactionBase (Connection connection ) {
57
59
this (connection , true );
@@ -67,7 +69,22 @@ public TransactionBase(Connection connection) {
67
69
* @param doMulti {@code false} should be set to enable manual WATCH, UNWATCH and MULTI
68
70
*/
69
71
public TransactionBase (Connection connection , boolean doMulti ) {
72
+ this (connection , doMulti , false );
73
+ }
74
+
75
+ /**
76
+ * Creates a new transaction.
77
+ *
78
+ * A user wanting to WATCH/UNWATCH keys followed by a call to MULTI ({@link #multi()}) it should
79
+ * be {@code doMulti=false}.
80
+ *
81
+ * @param connection connection
82
+ * @param doMulti {@code false} should be set to enable manual WATCH, UNWATCH and MULTI
83
+ * @param closeConnection should the 'connection' be closed when 'close()' is called?
84
+ */
85
+ public TransactionBase (Connection connection , boolean doMulti , boolean closeConnection ) {
70
86
this .connection = connection ;
87
+ this .closeConnection = closeConnection ;
71
88
this .commandObjects = new CommandObjects ();
72
89
this .graphCommandObjects = new GraphCommandObjects (this .connection );
73
90
if (doMulti ) multi ();
@@ -112,7 +129,13 @@ protected final <T> Response<T> appendCommand(CommandObject<T> commandObject) {
112
129
113
130
@ Override
114
131
public final void close () {
115
- clear ();
132
+ try {
133
+ clear ();
134
+ } finally {
135
+ if (closeConnection ) {
136
+ connection .close ();
137
+ }
138
+ }
116
139
}
117
140
118
141
public final void clear () {
0 commit comments