Friday, April 26, 2024
HomeJavaNested Transactions in jOOQ – Java, SQL and jOOQ.

Nested Transactions in jOOQ – Java, SQL and jOOQ.


Since jOOQ 3.4, we’ve got an API that simplifies transactional logic on high of JDBC in jOOQ, and ranging from jOOQ 3.17 and #13502, an equal API can even be made obtainable on high of R2DBC, for reactive purposes.

As with every thing jOOQ, transactions are carried out utilizing specific, API primarily based logic. The implicit logic carried out in Jakarta EE and Spring works nice for these platforms, which use annotations and elements all over the place, however the annotation-based paradigm doesn’t match jOOQ properly.

This text exhibits how jOOQ designed the transaction API, and why the Spring Propagation.NESTED semantics is the default in jOOQ.

Following JDBC’s defaults

In JDBC (as a lot as in R2DBC), a standalone assertion is at all times non-transactional, or auto-committing. The identical is true for jOOQ. If you happen to cross a non-transactional JDBC connection to jOOQ, a question like this will probably be auto-committing as properly:

ctx.insertInto(BOOK)
   .columns(BOOK.ID, BOOK.TITLE)
   .values(1, "Starting jOOQ")
   .values(2, "jOOQ Masterclass")
   .execute();

To date so good, this has been an inexpensive default in most APIs. However often, you don’t auto-commit. You write transactional logic.

Transactional lambdas

If you wish to run a number of statements in a single transaction, you may write this in jOOQ:

// The transaction() name wraps a transaction
ctx.transaction(trx -> {

    // The entire lambda expression is the transaction's content material
    trx.dsl()
       .insertInto(AUTHOR)
       .columns(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
       .values(1, "Tayo", "Koleoso")
       .values(2, "Anghel", "Leonard")
       .execute();

    trx.dsl()
       .insertInto(BOOK)
       .columns(BOOK.ID, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(1, 1, "Starting jOOQ")
       .values(2, 2, "jOOQ Masterclass")
       .execute();

    // If the lambda is accomplished usually, we commit
    // If there's an exception, we rollback
});

The psychological mannequin is strictly the identical as with Jakarta EE and Spring @Transactional elements. Regular completion implicitly commits, distinctive completion implicitly rolls again. The entire lambda is an atomic “unit of labor,” which is fairly intuitive.

You personal your management circulate

If there’s any recoverable exception inside your code, you might be allowed to deal with that gracefully, and jOOQ’s transaction administration received’t discover. For instance:

ctx.transaction(trx -> {
    strive {
        trx.dsl()
           .insertInto(AUTHOR)
           .columns(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
           .values(1, "Tayo", "Koleoso")
           .values(2, "Anghel", "Leonard")
           .execute();
    }
    catch (DataAccessException e) {

        // Re-throw all non-constraint violation exceptions
        if (e.sqlStateClass() != C23_INTEGRITY_CONSTRAINT_VIOLATION)
            throw e;

        // Ignore if we have already got the authors
    }

    // If we had a constraint violation above, we are able to proceed our
    // work right here. The transaction is not rolled again
    trx.dsl()
       .insertInto(BOOK)
       .columns(BOOK.ID, BOOK.AUTHOR_ID, BOOK.TITLE)
       .values(1, 1, "Starting jOOQ")
       .values(2, 2, "jOOQ Masterclass")
       .execute();
});

The identical is true in most different APIs, together with Spring. If Spring is unaware of your exceptions, it is not going to interpret these exceptions for transactional logic, which makes excellent sense. In spite of everything, any third occasion library might throw and catch inside exceptions with out you noticing, so why ought to Spring discover.

Transaction propagation

Jakarta EE and Spring provide quite a lot of transaction propagation modes (TxType in Jakarta EE, Propagation in Spring). The default in each is REQUIRED. I’ve been attempting to analysis why REQUIRED is the default, and never NESTED, which I discover rather more logical and proper, as I’ll clarify afterwards. If you realize, please let me know on twitter or within the feedback:

My assumption for these APIs is

  1. NESTED requires SAVEPOINT help, which isn’t obtainable in all RDBMS that help transactions
  2. REQUIRED avoids SAVEPOINT overhead, which could be a drawback for those who don’t really have to nest transactions (though we would argue that the API is then wrongly annotated with too many incidental @Transactional annotations. Similar to you shouldn’t mindlessly run SELECT *, you shouldn’t annotate every thing with out giving issues sufficient thought.)
  3. It’s not unlikely that in Spring consumer code, each service methodology is simply blindly annotated with @Transactional with out giving this matter an excessive amount of thought (identical as error dealing with), after which, making transactions REQUIRED as a substitute of NESTED would simply be a extra handy default “to make it work.” That will be in favour of REQUIRED being extra of an incidental default than a properly chosen one.
  4. JPA can’t really work properly with NESTED transactions, as a result of the entities develop into corrupt (see Vlad’s touch upon this). In my view, that’s only a bug or lacking function, although I can see that implementing the function may be very complicated and maybe not price it in JPA.

So, for all of those merely technical causes, it appears to be comprehensible for APIs like Jakarta EE or Spring to not make NESTED the default (Jakarta EE doesn’t even help it in any respect).

However that is jOOQ and jOOQ has at all times been taking a step again to consider how issues ought to be, fairly than being impressed with how issues are.

When you concentrate on the next code:

@Transactional
void tx() {
    tx1();

    strive {
        tx2();
    }
    catch (Exception e) {
        log.information(e);
    }

    continueWorkOnTx1();
}

@Transactional
void tx1() { ... }

@Transactional
void tx2() { ... }

The intent of the programmer who wrote that code can solely be one factor:

  • Begin a world transaction in tx()
  • Do some nested transactional work in tx1()
  • Attempt doing another nested transactional work in tx2()
    • If tx2() succeeds, superb, transfer on
    • If tx2() fails, simply log the error, ROLLBACK to earlier than tx2(), and transfer on
  • Regardless of tx2(), proceed working with tx1()‘s (and presumably additionally tx2()‘s) end result

However this isn’t what REQUIRED, which is the default in Jakarta EE and Spring, will do. It’s going to simply rollback tx2() and tx1(), leaving the outer transaction in a really bizarre state, that means that continueWorkOnTx1() will fail. However ought to it actually fail? tx2() was alleged to be an atomic unit of labor, unbiased of who referred to as it. It isn’t, by default, so the Exception e should be propagated. The one factor that may be finished within the catch block, earlier than mandatorily rethrowing, is clear up some sources or do some logging. (Good luck ensuring each dev follows these guidelines!)

And, as soon as we mandatorily rethrow, REQUIRED turns into successfully the identical as NESTED, besides there aren’t any extra savepoints. So, the default is:

  • The identical as NESTED within the blissful path
  • Bizarre within the not so blissful path

Which is a robust argument in favour of constructing NESTED the default, at the very least in jOOQ. Now, the linked twitter dialogue digressed fairly a bit into architectural issues of why:

  • NESTED is a nasty concept or doesn’t work all over the place
  • Pessimistic locking is a nasty concept
  • and so on.

I don’t disagree with a lot of these arguments. But, focusing solely on the listed code, and placing myself within the footwear of a library developer, what might the programmer have presumably supposed by this code? I can’t see something different that Spring’s NESTED transaction semantics. I merely can’t.

jOOQ implements NESTED semantics

For the above causes, jOOQ’s transactions implement solely Spring’s NESTED semantics if savepoints are supported, or fail nesting totally in the event that they’re not supported (weirdly, this isn’t an choice in both Jakarta EE and Spring, as that might be one other cheap default). The distinction to Spring being, once more, that every thing is completed programmatically and explicitly, fairly than implicitly utilizing elements.

For instance:

ctx.transaction(trx -> {
    trx.dsl().transaction(trx1 -> {
        // ..
    });

    strive {
        trx.dsl().transaction(trx2 -> {
            // ..
        });
    }
    catch (Exception e) {
        log.information(e);
    }

    continueWorkOnTrx1(trx);
});

If trx2 fails with an exception, solely trx2 is rolled again. Not trx1. In fact, you may nonetheless re-throw the exception to roll again every thing. However the stance right here is that for those who, the programmer, inform jOOQ to run a nested transaction, properly, jOOQ will obey, as a result of that’s what you need.

You couldn’t presumably need the rest, as a result of then, you’d simply not nest the transaction within the first place, no?

R2DBC transactions

As talked about earlier, jOOQ 3.17 will (lastly) help transactions additionally in R2DBC. The semantics is strictly the identical as with JDBC’s blocking APIs, besides that every thing is now a Writer. So, now you can write:

Flux<?> flux = Flux.from(ctx.transactionPublisher(trx -> Flux
    .from(trx.dsl()
        .insertInto(AUTHOR)
        .columns(AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
        .values(1, "Tayo", "Koleoso")
        .values(2, "Anghel", "Leonard"))
    .thenMany(trx.dsl()
        .insertInto(BOOK)
        .columns(BOOK.ID, BOOK.AUTHOR_ID, BOOK.TITLE)
        .values(1, 1, "Starting jOOQ")
        .values(2, 2, "jOOQ Masterclass"))
}));

The instance makes use of reactor as a reactive streams API implementation, however you too can use RxJava, Mutiny, or no matter. The instance works precisely the identical because the JDBC one, initially.

Nesting additionally works the identical means, within the traditional, reactive (i.e. extra laborious) means:

Flux<?> flux = Flux.from(ctx.transactionPublisher(trx -> Flux
    .from(trx.dsl().transactionPublisher(trx1 -> { ... }))
    .thenMany(Flux
        .from(trx.dsl().transactionPublisher(trx2 -> { ... }))
        .onErrorContinue((e, t) -> log.information(e)))
    .thenMany(continueWorkOnTrx1(trx))
));

The sequencing utilizing thenMany() is only one instance. You could discover a want for totally totally different stream constructing primitives, which aren’t strictly associated to transaction administration.

Conclusion

Nesting transactions is sometimes helpful. With jOOQ, transaction propagation is far much less of a subject than with Jakarta EE or Spring as every thing you do is often specific, and as such, you don’t by chance nest transactions, if you do, you do it deliberately. This is the reason jOOQ opted for a unique default than Spring, and one which Jakarta EE doesn’t help in any respect. The Propagation.NESTED semantics, which is a robust solution to hold the laborious savepoint associated logic out of your code.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments