@@ -27,42 +27,48 @@ public void run() {
27
27
// REMOVE_END
28
28
29
29
// 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 ()) {
31
33
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 ();
34
39
}
35
40
36
- pipe . sync ();
41
+ try ( AbstractPipeline pipe = jedis . pipelined ()) {
37
42
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" );
39
46
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 ();
43
48
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
45
53
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
+ }
50
61
// 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
56
62
57
63
// 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 ()) {
63
65
64
- trans .exec ();
66
+ trans .incrBy ("counter:1" , 1 );
67
+ trans .incrBy ("counter:2" , 2 );
68
+ trans .incrBy ("counter:3" , 3 );
65
69
70
+ trans .exec ();
71
+ }
66
72
System .out .println (jedis .get ("counter:1" )); // >>> 1
67
73
System .out .println (jedis .get ("counter:2" )); // >>> 2
68
74
System .out .println (jedis .get ("counter:3" )); // >>> 3
@@ -78,40 +84,41 @@ public void run() {
78
84
jedis .set ("shellpath" , "/usr/syscmds/" );
79
85
80
86
// 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/
102
113
}
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
107
120
}
108
121
// 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
115
122
116
123
// HIDE_START
117
124
jedis .close ();
0 commit comments