Vert.x 4 introduces a simplified transaction API: https://vertx-web-site.github.io/docs/vertx-pg-client/java/#_simplified_transaction_api
This is the code of the withTransaction method: https://github.com/eclipse-vertx/vertx-sql-client/blob/4.0.0.Beta3/vertx-sql-client/src/main/java/io/vertx/sqlclient/Pool.java#L132-L172
It gets a connection from the pool.
It opens a transaction (BEGIN).
It calls the provided function.
If the function succeeds the Future then withTransaction closes the transaction with COMMIT.
If the function fails the Future then withTransaction closes the transaction with ROLLBACK.
Then withTransaction returns the connection to the pool.
This is a good design:
- It guarantees that the transaction is closed in all cases. This is important when connections are returned to the pool and reused - if there is still an open transaction this fails when another BEGIN for a new transaction is called.
- It guarantees that the connection is returned to the pool in all cases.
- It fits into the Futurisation efforts.
Vert.x 4 has breaking changes in the regular transaction API, see
RMB-609 pull request. We should consider implementing this simplified transaction API in RMB to allow modules based on RMB to immediately switch to it.
Also add the similar withConnection method that also automatically returns the connection to the pool but doesn't do any BEGIN, COMMIT, ROLLBACK.