Created
April 9, 2020 14:21
-
-
Save vietj/75ddfd3294c6958792a2d2ac6c4cc865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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