Skip to content

Commit 582df92

Browse files
Update "Pipelines and transactions" doc example to include pipeline.close() (#4134)
* Update pipeline/transaction examples in documentation to ensure proper transaction/pipeline resource closure closes #4132 * Update src/test/java/io/redis/examples/PipeTransExample.java Co-authored-by: andy-stark-redis <[email protected]> --------- Co-authored-by: andy-stark-redis <[email protected]>
1 parent 856a3b0 commit 582df92

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

src/test/java/io/redis/examples/PipeTransExample.java

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,48 @@ public void run() {
2727
// REMOVE_END
2828

2929
// STEP_START basic_pipe
30-
AbstractPipeline pipe = jedis.pipelined();
30+
// Make sure you close the pipeline after use to release resources
31+
// and return the connection to the pool.
32+
try (AbstractPipeline pipe = jedis.pipelined()) {
3133

32-
for (int i = 0; i < 5; i++) {
33-
pipe.set(String.format("seat:%d", i), String.format("#%d", i));
34+
for (int i = 0; i < 5; i++) {
35+
pipe.set(String.format("seat:%d", i), String.format("#%d", i));
36+
}
37+
38+
pipe.sync();
3439
}
3540

36-
pipe.sync();
41+
try (AbstractPipeline pipe = jedis.pipelined()) {
3742

38-
pipe = jedis.pipelined();
43+
Response<String> resp0 = pipe.get("seat:0");
44+
Response<String> resp3 = pipe.get("seat:3");
45+
Response<String> resp4 = pipe.get("seat:4");
3946

40-
Response<String> resp0 = pipe.get("seat:0");
41-
Response<String> resp3 = pipe.get("seat:3");
42-
Response<String> resp4 = pipe.get("seat:4");
47+
pipe.sync();
4348

44-
pipe.sync();
49+
// Responses are available after the pipeline has executed.
50+
System.out.println(resp0.get()); // >>> #0
51+
System.out.println(resp3.get()); // >>> #3
52+
System.out.println(resp4.get()); // >>> #4
4553

46-
// Responses are available after the pipeline has executed.
47-
System.out.println(resp0.get()); // >>> #0
48-
System.out.println(resp3.get()); // >>> #3
49-
System.out.println(resp4.get()); // >>> #4
54+
55+
// REMOVE_START
56+
Assert.assertEquals("#0", resp0.get());
57+
Assert.assertEquals("#3", resp3.get());
58+
Assert.assertEquals("#4", resp4.get());
59+
// REMOVE_END
60+
}
5061
// STEP_END
51-
// REMOVE_START
52-
Assert.assertEquals("#0", resp0.get());
53-
Assert.assertEquals("#3", resp3.get());
54-
Assert.assertEquals("#4", resp4.get());
55-
// REMOVE_END
5662

5763
// STEP_START basic_trans
58-
AbstractTransaction trans = jedis.multi();
59-
60-
trans.incrBy("counter:1", 1);
61-
trans.incrBy("counter:2", 2);
62-
trans.incrBy("counter:3", 3);
64+
try ( AbstractTransaction trans = jedis.multi()) {
6365

64-
trans.exec();
66+
trans.incrBy("counter:1", 1);
67+
trans.incrBy("counter:2", 2);
68+
trans.incrBy("counter:3", 3);
6569

70+
trans.exec();
71+
}
6672
System.out.println(jedis.get("counter:1")); // >>> 1
6773
System.out.println(jedis.get("counter:2")); // >>> 2
6874
System.out.println(jedis.get("counter:3")); // >>> 3
@@ -78,40 +84,41 @@ public void run() {
7884
jedis.set("shellpath", "/usr/syscmds/");
7985

8086
// Start the transaction and watch the key we are about to update.
81-
trans = jedis.transaction(false); // create a Transaction object without sending MULTI command
82-
trans.watch("shellpath"); // send WATCH command(s)
83-
trans.multi(); // send MULTI command
84-
85-
String currentPath = jedis.get("shellpath");
86-
String newPath = currentPath + ":/usr/mycmds/";
87-
88-
// Commands added to the `trans` object
89-
// will be buffered until `trans.exec()` is called.
90-
Response<String> setResult = trans.set("shellpath", newPath);
91-
List<Object> transResults = trans.exec();
92-
93-
// The `exec()` call returns null if the transaction failed.
94-
if (transResults != null) {
95-
// Responses are available if the transaction succeeded.
96-
System.out.println(setResult.get()); // >>> OK
97-
98-
// You can also get the results from the list returned by
99-
// `trans.exec()`.
100-
for (Object item: transResults) {
101-
System.out.println(item);
87+
try (AbstractTransaction trans = jedis.transaction(false)) { // create a Transaction object without sending MULTI command
88+
trans.watch("shellpath"); // send WATCH command(s)
89+
trans.multi(); // send MULTI command
90+
91+
String currentPath = jedis.get("shellpath");
92+
String newPath = currentPath + ":/usr/mycmds/";
93+
94+
// Commands added to the `trans` object
95+
// will be buffered until `trans.exec()` is called.
96+
Response<String> setResult = trans.set("shellpath", newPath);
97+
List<Object> transResults = trans.exec();
98+
99+
// The `exec()` call returns null if the transaction failed.
100+
if (transResults != null) {
101+
// Responses are available if the transaction succeeded.
102+
System.out.println(setResult.get()); // >>> OK
103+
104+
// You can also get the results from the list returned by
105+
// `trans.exec()`.
106+
for (Object item: transResults) {
107+
System.out.println(item);
108+
}
109+
// >>> OK
110+
111+
System.out.println(jedis.get("shellpath"));
112+
// >>> /usr/syscmds/:/usr/mycmds/
102113
}
103-
// >>> OK
104-
105-
System.out.println(jedis.get("shellpath"));
106-
// >>> /usr/syscmds/:/usr/mycmds/
114+
// REMOVE_START
115+
Assert.assertEquals("/usr/syscmds/:/usr/mycmds/", jedis.get("shellpath"));
116+
Assert.assertEquals("OK", setResult.get());
117+
Assert.assertEquals(1, transResults.size());
118+
Assert.assertEquals("OK", transResults.get(0).toString());
119+
// REMOVE_END
107120
}
108121
// STEP_END
109-
// REMOVE_START
110-
Assert.assertEquals("/usr/syscmds/:/usr/mycmds/", jedis.get("shellpath"));
111-
Assert.assertEquals("OK", setResult.get());
112-
Assert.assertEquals(1, transResults.size());
113-
Assert.assertEquals("OK", transResults.get(0).toString());
114-
// REMOVE_END
115122

116123
// HIDE_START
117124
jedis.close();

0 commit comments

Comments
 (0)