pool.getConnection(res -> { if (res.succeeded()) {
// Transaction must use a connection PgConnection conn = res.result();
// Begin the transaction PgTransaction tx = conn.begin();
// Various statements conn.query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')", ar -> {}); conn.query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')", ar -> {});
// Return the connectionto the pool conn.close(); }); } });
再看第二种:
pool.begin(res -> { if (res.succeeded()) {
// Get the transaction PgTransaction tx = res.result();
// Various statements tx.query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')", ar -> {}); tx.query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')", ar -> {});
// Commit the transactionandreturn the connectionto the pool tx.commit(ar -> { if (ar.succeeded()) { System.out.println("Transaction succeeded"); } else { System.out.println("Transaction failed " + ar.cause().getMessage()); } }); } });