kotlin-coroutinesjooqr2dbc

How to programmatically rollback transaction using Jooq Non blocking API?


It's not documented here https://www.jooq.org/doc/latest/manual/sql-execution/transaction-management/

It only shows you that an uncaught exception will cause a rollback.

The problem is that RollbackToSavepointStep is not a Publisher.

Is it possible to programmatically rollback at all?

If I try dsl().rollback().executeAsync().await() then I get:

DetachedException: Attempt to execute a blocking method (e.g. Query.execute() or ResultQuery.fetch()) when only an R2BDC ConnectionFactory was configured. jOOQ's RowCountQuery and ResultQuery extend Publisher, which allows for reactive streams implementations to subscribe to the results of a jOOQ query. Simply embed your query in the stream, e.g. using Flux.from(query). See also: https://www.jooq.org/doc/latest/manual/sql-execution/fetching/reactive-fetching/

Solution

  • You can use the ROLLBACK statement and execute that reactively. Just not with executeAsync(), which currently doesn't have any R2DBC backed implementation as the exception states. There's a feature request for this as of jOOQ 3.19:

    Instead, you should be able to just write:

    dsl().rollback().awaitSingle()
    

    Note, this currently doesn't work because of this bug in jOOQ 3.19.18 and earlier:

    The Rollback type extends only Query, not RowCountQuery, which is a Publisher<Integer>. Given that this is just a matter of wrong API specification, do this, instead, as a workaround:

    (dsl().rollback() as RowCountQuery).awaitSingle()