Skip to content

Commit

Permalink
Merge pull request #20033 from tsegismont/reactive-tx-doc
Browse files Browse the repository at this point in the history
Fix outdated reactive transaction management doc
  • Loading branch information
cescoffier authored Sep 10, 2021
2 parents 9c7df3e + 591445a commit 70e5b80
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions docs/src/main/asciidoc/reactive-sql-clients.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -492,29 +492,29 @@ Navigate to http://localhost:8080/fruits.html and read/create/delete some fruits
== Transactions

The reactive SQL clients support transactions.
A transaction is started with `client.begin()` and terminated with either `tx.commit()` or `tx.rollback()`.
A transaction is started with `io.vertx.mutiny.sqlclient.SqlConnection#begin` and terminated with either `io.vertx.mutiny.sqlclient.Transaction#commit` or `io.vertx.mutiny.sqlclient.Transaction#rollback`.
All these operations are asynchronous:

* `client.begin()` returns a `Uni<Transaction>`
* `client.commit()` and `client.rollback()` return `Uni<Void>`
* `connection.begin()` returns a `Uni<Transaction>`
* `transaction.commit()` and `transaction.rollback()` return `Uni<Void>`

Managing transactions in the reactive programming world can be cumbersome.
Instead of writing repetitive and complex (thus error-prone!) code, you can use the `io.vertx.mutiny.sqlclient.SqlClientHelper`.
Instead of writing repetitive and complex (thus error-prone!) code, you can use the `io.vertx.mutiny.sqlclient.Pool#withTransaction` helper method.

The following snippet shows how to run 2 insertions in the same transaction:

[source, java]
----
public static Uni<Void> insertTwoFruits(PgPool client, Fruit fruit1, Fruit fruit2) {
return SqlClientHelper.inTransactionUni(client, tx -> {
Uni<RowSet<Row>> insertOne = tx.preparedQuery("INSERT INTO fruits (name) VALUES ($1) RETURNING id")
return client.withTransaction(conn -> {
Uni<RowSet<Row>> insertOne = conn.preparedQuery("INSERT INTO fruits (name) VALUES ($1) RETURNING id")
.execute(Tuple.of(fruit1.name));
Uni<RowSet<Row>> insertTwo = tx.preparedQuery("INSERT INTO fruits (name) VALUES ($1) RETURNING id")
Uni<RowSet<Row>> insertTwo = conn.preparedQuery("INSERT INTO fruits (name) VALUES ($1) RETURNING id")
.execute(Tuple.of(fruit2.name));
return insertOne.and(insertTwo)
return Uni.combine().all().unis(insertOne, insertTwo)
// Ignore the results (the two ids)
.onItem().ignore().andContinueWithNull();
.discardItems();
});
}
----
Expand All @@ -525,12 +525,12 @@ You can also create dependent actions as follows:

[source, java]
----
return SqlClientHelper.inTransactionUni(client, tx -> tx
return client.withTransaction(conn -> conn
.preparedQuery("INSERT INTO person (firstname,lastname) VALUES ($1,$2) RETURNING id")
.execute(Tuple.of(person.getFirstName(), person.getLastName()))
.execute(Tuple.of(person.getFirstName(), person.getLastName()))
.onItem().transformToUni(id -> tx.preparedQuery("INSERT INTO addr (person_id,addrline1) VALUES ($1,$2)")
.onItem().transformToUni(id -> conn.preparedQuery("INSERT INTO addr (person_id,addrline1) VALUES ($1,$2)")
.execute(Tuple.of(id.iterator().next().getLong("id"), person.getLastName())))
.onItem().ignore().andContinueWithNull());
Expand Down

0 comments on commit 70e5b80

Please sign in to comment.