Skip to content

Instantly share code, notes, and snippets.

@vietj
Created April 9, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vietj/75ddfd3294c6958792a2d2ac6c4cc865 to your computer and use it in GitHub Desktop.
Save vietj/75ddfd3294c6958792a2d2ac6c4cc865 to your computer and use it in GitHub Desktop.
pool.begin(res -> {
if (res.succeeded()) {
// Get the transaction
Transaction tx = res.result();
// Various statements
tx.query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')")
.execute(ar1 -> {
if (ar1.succeeded()) {
tx.query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')")
.execute(ar2 -> {
if (ar2.succeeded()) {
// Commit the transaction
// the connection will automatically return to the pool
tx.commit(ar3 -> {
if (ar3.succeeded()) {
System.out.println("Transaction succeeded");
} else {
System.out.println("Transaction failed " + ar3.cause().getMessage());
}
});
}
});
} else {
// No need to close connection as transaction will abort and be returned to the pool
}
});
}
});
pool.begin()
.flatMap(tx -> tx
.query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')")
.execute()
.flatMap(res1 -> tx
.query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')")
.execute())
.compose(
res2 -> tx.commit(),
err -> tx.rollback())
).onComplete(ar3 -> {
if (ar3.succeeded()) {
System.out.println("Transaction succeeded");
} else {
System.out.println("Transaction failed " + ar3.cause().getMessage());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment