From 42e5cc512415daa7145cddc786e0f4937b13f9f5 Mon Sep 17 00:00:00 2001 From: ggivo Date: Sat, 5 Apr 2025 23:02:45 +0300 Subject: [PATCH 1/2] Update pipeline/transaction examples in documentation to ensure proper transaction/pipeline resource closure closes #4132 --- .../io/redis/examples/PipeTransExample.java | 118 ++++++++++-------- 1 file changed, 63 insertions(+), 55 deletions(-) diff --git a/src/test/java/io/redis/examples/PipeTransExample.java b/src/test/java/io/redis/examples/PipeTransExample.java index 353e6288d5..a73bcd6c66 100644 --- a/src/test/java/io/redis/examples/PipeTransExample.java +++ b/src/test/java/io/redis/examples/PipeTransExample.java @@ -27,42 +27,49 @@ public void run() { // REMOVE_END // STEP_START basic_pipe - AbstractPipeline pipe = jedis.pipelined(); + // Pipeline is a way to send multiple commands to Redis in a single request. + // It can be used to improve performance by reducing the number of round trips to the server. + // Make sure to close the pipeline after use to release resources and return the connection to the pool. + try (AbstractPipeline pipe = jedis.pipelined()) { - for (int i = 0; i < 5; i++) { - pipe.set(String.format("seat:%d", i), String.format("#%d", i)); + for (int i = 0; i < 5; i++) { + pipe.set(String.format("seat:%d", i), String.format("#%d", i)); + } + + pipe.sync(); } - pipe.sync(); + try (AbstractPipeline pipe = jedis.pipelined()) { - pipe = jedis.pipelined(); + Response resp0 = pipe.get("seat:0"); + Response resp3 = pipe.get("seat:3"); + Response resp4 = pipe.get("seat:4"); - Response resp0 = pipe.get("seat:0"); - Response resp3 = pipe.get("seat:3"); - Response resp4 = pipe.get("seat:4"); + pipe.sync(); - pipe.sync(); + // Responses are available after the pipeline has executed. + System.out.println(resp0.get()); // >>> #0 + System.out.println(resp3.get()); // >>> #3 + System.out.println(resp4.get()); // >>> #4 - // Responses are available after the pipeline has executed. - System.out.println(resp0.get()); // >>> #0 - System.out.println(resp3.get()); // >>> #3 - System.out.println(resp4.get()); // >>> #4 + + // REMOVE_START + Assert.assertEquals("#0", resp0.get()); + Assert.assertEquals("#3", resp3.get()); + Assert.assertEquals("#4", resp4.get()); + // REMOVE_END + } // STEP_END - // REMOVE_START - Assert.assertEquals("#0", resp0.get()); - Assert.assertEquals("#3", resp3.get()); - Assert.assertEquals("#4", resp4.get()); - // REMOVE_END // STEP_START basic_trans - AbstractTransaction trans = jedis.multi(); - - trans.incrBy("counter:1", 1); - trans.incrBy("counter:2", 2); - trans.incrBy("counter:3", 3); + try ( AbstractTransaction trans = jedis.multi()) { - trans.exec(); + trans.incrBy("counter:1", 1); + trans.incrBy("counter:2", 2); + trans.incrBy("counter:3", 3); + trans.exec(); + } System.out.println(jedis.get("counter:1")); // >>> 1 System.out.println(jedis.get("counter:2")); // >>> 2 System.out.println(jedis.get("counter:3")); // >>> 3 @@ -78,40 +85,41 @@ public void run() { jedis.set("shellpath", "/usr/syscmds/"); // Start the transaction and watch the key we are about to update. - trans = jedis.transaction(false); // create a Transaction object without sending MULTI command - trans.watch("shellpath"); // send WATCH command(s) - trans.multi(); // send MULTI command - - String currentPath = jedis.get("shellpath"); - String newPath = currentPath + ":/usr/mycmds/"; - - // Commands added to the `trans` object - // will be buffered until `trans.exec()` is called. - Response setResult = trans.set("shellpath", newPath); - List transResults = trans.exec(); - - // The `exec()` call returns null if the transaction failed. - if (transResults != null) { - // Responses are available if the transaction succeeded. - System.out.println(setResult.get()); // >>> OK - - // You can also get the results from the list returned by - // `trans.exec()`. - for (Object item: transResults) { - System.out.println(item); + try (AbstractTransaction trans = jedis.transaction(false)) { // create a Transaction object without sending MULTI command + trans.watch("shellpath"); // send WATCH command(s) + trans.multi(); // send MULTI command + + String currentPath = jedis.get("shellpath"); + String newPath = currentPath + ":/usr/mycmds/"; + + // Commands added to the `trans` object + // will be buffered until `trans.exec()` is called. + Response setResult = trans.set("shellpath", newPath); + List transResults = trans.exec(); + + // The `exec()` call returns null if the transaction failed. + if (transResults != null) { + // Responses are available if the transaction succeeded. + System.out.println(setResult.get()); // >>> OK + + // You can also get the results from the list returned by + // `trans.exec()`. + for (Object item: transResults) { + System.out.println(item); + } + // >>> OK + + System.out.println(jedis.get("shellpath")); + // >>> /usr/syscmds/:/usr/mycmds/ } - // >>> OK - - System.out.println(jedis.get("shellpath")); - // >>> /usr/syscmds/:/usr/mycmds/ + // REMOVE_START + Assert.assertEquals("/usr/syscmds/:/usr/mycmds/", jedis.get("shellpath")); + Assert.assertEquals("OK", setResult.get()); + Assert.assertEquals(1, transResults.size()); + Assert.assertEquals("OK", transResults.get(0).toString()); + // REMOVE_END } // STEP_END - // REMOVE_START - Assert.assertEquals("/usr/syscmds/:/usr/mycmds/", jedis.get("shellpath")); - Assert.assertEquals("OK", setResult.get()); - Assert.assertEquals(1, transResults.size()); - Assert.assertEquals("OK", transResults.get(0).toString()); - // REMOVE_END // HIDE_START jedis.close(); From debacfa0704334ebee73cd28126ed0479820de9e Mon Sep 17 00:00:00 2001 From: ggivo Date: Mon, 7 Apr 2025 13:09:59 +0300 Subject: [PATCH 2/2] Update src/test/java/io/redis/examples/PipeTransExample.java Co-authored-by: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> --- src/test/java/io/redis/examples/PipeTransExample.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/redis/examples/PipeTransExample.java b/src/test/java/io/redis/examples/PipeTransExample.java index a73bcd6c66..c5dd62bf7e 100644 --- a/src/test/java/io/redis/examples/PipeTransExample.java +++ b/src/test/java/io/redis/examples/PipeTransExample.java @@ -27,9 +27,8 @@ public void run() { // REMOVE_END // STEP_START basic_pipe - // Pipeline is a way to send multiple commands to Redis in a single request. - // It can be used to improve performance by reducing the number of round trips to the server. - // Make sure to close the pipeline after use to release resources and return the connection to the pool. + // Make sure you close the pipeline after use to release resources + // and return the connection to the pool. try (AbstractPipeline pipe = jedis.pipelined()) { for (int i = 0; i < 5; i++) {