@@ -210,32 +210,38 @@ public void transaction01(Pool pool) {
210
210
SqlConnection conn = res .result ();
211
211
212
212
// Begin the transaction
213
- Transaction tx = conn .begin ();
214
-
215
- // Various statements
216
- conn
217
- .query ("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')" )
218
- .execute (ar1 -> {
219
- if (ar1 .succeeded ()) {
213
+ conn .begin (ar0 -> {
214
+ if (ar0 .succeeded ()) {
215
+ Transaction tx = ar0 .result ();
216
+ // Various statements
220
217
conn
221
- .query ("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')" )
222
- .execute (ar2 -> {
223
- if (ar2 .succeeded ()) {
224
- // Commit the transaction
225
- tx .commit (ar3 -> {
226
- if (ar3 .succeeded ()) {
227
- System .out .println ("Transaction succeeded" );
228
- } else {
229
- System .out .println ("Transaction failed " + ar3 .cause ().getMessage ());
230
- }
218
+ .query ("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')" )
219
+ .execute (ar1 -> {
220
+ if (ar1 .succeeded ()) {
221
+ conn
222
+ .query ("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')" )
223
+ .execute (ar2 -> {
224
+ if (ar2 .succeeded ()) {
225
+ // Commit the transaction
226
+ tx .commit (ar3 -> {
227
+ if (ar3 .succeeded ()) {
228
+ System .out .println ("Transaction succeeded" );
229
+ } else {
230
+ System .out .println ("Transaction failed " + ar3 .cause ().getMessage ());
231
+ }
232
+ // Return the connection to the pool
233
+ conn .close ();
234
+ });
235
+ } else {
236
+ // Return the connection to the pool
237
+ conn .close ();
238
+ }
239
+ });
240
+ } else {
231
241
// Return the connection to the pool
232
242
conn .close ();
233
- });
234
- } else {
235
- // Return the connection to the pool
236
- conn .close ();
237
- }
238
- });
243
+ }
244
+ });
239
245
} else {
240
246
// Return the connection to the pool
241
247
conn .close ();
@@ -246,69 +252,62 @@ public void transaction01(Pool pool) {
246
252
}
247
253
248
254
public void transaction02 (Transaction tx ) {
249
- tx .abortHandler ( v -> {
255
+ tx .completion (). onFailure ( err -> {
250
256
System .out .println ("Transaction failed => rollbacked" );
251
257
});
252
258
}
253
259
254
260
public void transaction03 (Pool pool ) {
255
261
256
262
// Acquire a transaction and begin the transaction
257
- pool .begin (res -> {
258
- if (res .succeeded ()) {
259
-
260
- // Get the transaction
261
- Transaction tx = res .result ();
262
-
263
- // Various statements
264
- tx .query ("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')" )
265
- .execute (ar1 -> {
266
- if (ar1 .succeeded ()) {
267
- tx .query ("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')" )
268
- .execute (ar2 -> {
269
- if (ar2 .succeeded ()) {
270
- // Commit the transaction
271
- // the connection will automatically return to the pool
272
- tx .commit (ar3 -> {
273
- if (ar3 .succeeded ()) {
274
- System .out .println ("Transaction succeeded" );
275
- } else {
276
- System .out .println ("Transaction failed " + ar3 .cause ().getMessage ());
277
- }
278
- });
279
- }
280
- });
281
- } else {
282
- // No need to close connection as transaction will abort and be returned to the pool
283
- }
284
- });
263
+ pool .withTransaction (client -> client
264
+ .query ("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')" )
265
+ .execute ()
266
+ .flatMap (res -> client
267
+ .query ("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')" )
268
+ .execute ()
269
+ // Map to a message result
270
+ .map ("Users inserted" ))
271
+ ).onComplete (ar -> {
272
+ // The connection was automatically return to the pool
273
+ if (ar .succeeded ()) {
274
+ // Transaction was committed
275
+ String message = ar .result ();
276
+ System .out .println ("Transaction succeeded: " + message );
277
+ } else {
278
+ // Transaction was rolled back
279
+ System .out .println ("Transaction failed " + ar .cause ().getMessage ());
285
280
}
286
281
});
287
282
}
288
283
289
284
public void usingCursors01 (SqlConnection connection ) {
290
- connection .prepare ("SELECT * FROM users WHERE first_name LIKE $1" , ar1 -> {
291
- if (ar1 .succeeded ()) {
292
- PreparedStatement pq = ar1 .result ();
285
+ connection .prepare ("SELECT * FROM users WHERE first_name LIKE $1" , ar0 -> {
286
+ if (ar0 .succeeded ()) {
287
+ PreparedStatement pq = ar0 .result ();
293
288
294
289
// Cursors require to run within a transaction
295
- Transaction tx = connection .begin ();
290
+ connection .begin (ar1 -> {
291
+ if (ar1 .succeeded ()) {
292
+ Transaction tx = ar1 .result ();
296
293
297
- // Create a cursor
298
- Cursor cursor = pq .cursor (Tuple .of ("julien" ));
294
+ // Create a cursor
295
+ Cursor cursor = pq .cursor (Tuple .of ("julien" ));
299
296
300
- // Read 50 rows
301
- cursor .read (50 , ar2 -> {
302
- if (ar2 .succeeded ()) {
303
- RowSet <Row > rows = ar2 .result ();
304
-
305
- // Check for more ?
306
- if (cursor .hasMore ()) {
307
- // Repeat the process...
308
- } else {
309
- // No more rows - commit the transaction
310
- tx .commit ();
311
- }
297
+ // Read 50 rows
298
+ cursor .read (50 , ar2 -> {
299
+ if (ar2 .succeeded ()) {
300
+ RowSet <Row > rows = ar2 .result ();
301
+
302
+ // Check for more ?
303
+ if (cursor .hasMore ()) {
304
+ // Repeat the process...
305
+ } else {
306
+ // No more rows - commit the transaction
307
+ tx .commit ();
308
+ }
309
+ }
310
+ });
312
311
}
313
312
});
314
313
}
@@ -325,26 +324,30 @@ public void usingCursors02(Cursor cursor) {
325
324
}
326
325
327
326
public void usingCursors03 (SqlConnection connection ) {
328
- connection .prepare ("SELECT * FROM users WHERE first_name LIKE $1" , ar1 -> {
329
- if (ar1 .succeeded ()) {
330
- PreparedStatement pq = ar1 .result ();
327
+ connection .prepare ("SELECT * FROM users WHERE first_name LIKE $1" , ar0 -> {
328
+ if (ar0 .succeeded ()) {
329
+ PreparedStatement pq = ar0 .result ();
331
330
332
331
// Streams require to run within a transaction
333
- Transaction tx = connection .begin ();
332
+ connection .begin (ar1 -> {
333
+ if (ar1 .succeeded ()) {
334
+ Transaction tx = ar1 .result ();
334
335
335
- // Fetch 50 rows at a time
336
- RowStream <Row > stream = pq .createStream (50 , Tuple .of ("julien" ));
336
+ // Fetch 50 rows at a time
337
+ RowStream <Row > stream = pq .createStream (50 , Tuple .of ("julien" ));
337
338
338
- // Use the stream
339
- stream .exceptionHandler (err -> {
340
- System .out .println ("Error: " + err .getMessage ());
341
- });
342
- stream .endHandler (v -> {
343
- tx .commit ();
344
- System .out .println ("End of stream" );
345
- });
346
- stream .handler (row -> {
347
- System .out .println ("User: " + row .getString ("last_name" ));
339
+ // Use the stream
340
+ stream .exceptionHandler (err -> {
341
+ System .out .println ("Error: " + err .getMessage ());
342
+ });
343
+ stream .endHandler (v -> {
344
+ tx .commit ();
345
+ System .out .println ("End of stream" );
346
+ });
347
+ stream .handler (row -> {
348
+ System .out .println ("User: " + row .getString ("last_name" ));
349
+ });
350
+ }
348
351
});
349
352
}
350
353
});
0 commit comments