0

In https://quarkus.io/guides/reactive-sql-clients page we have code snippet to execute query changes using transaction :

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

so here SqlClientHelper will begin the transaction,commit and rollback if any failure but is there any way to find out the root cause of the failure and print it in logs ? In the documentation its not mentioned how we can do that.

Pracheer Pancholi
  • 570
  • 2
  • 7
  • 21

2 Answers2

1

You can use Mutiny's onFailure to get the exception class and act on it. See this for more details.

geoand
  • 60,071
  • 24
  • 172
  • 190
1

based on the link as given in the accepted Answer this is working for me :

return SqlClientHelper.inTransactionUni(mysqlPool, tx -> {
        return tx.query(query).execute().onItem().transformToUni(
                id -> tx.query("SELECT TRAN_ID FROM " + tableName + " 
ORDER BY TO_DB_TS DESC LIMIT 1").execute())
                .onItem().transform(rows -> 
rows.iterator().next().getString(0)).onFailure().invoke(f -> {
                    LOG.error("Error while inserting data to " + 
tableName + " table::"+f.getMessage());
                });
    }); 
Pracheer Pancholi
  • 570
  • 2
  • 7
  • 21
  • MysqlPool class is not Autoclosable so do we need to explicitly call close() method from Pool class to close in case failure occurs or is it fine to just print the error message to the log and let SqlClientHelper rollback the entire transaction in case failure occurs.. – Pracheer Pancholi May 26 '21 at 16:41