QuarkusTransaction provides programmatic transaction management through static operations and wrappers for transactional Runnable or Callable code. It helps keep transaction boundaries explicit when work runs outside a CDI method, such as a gRPC stream callback. section 7.5.2 section 7.2.2
Use the API in two complementary ways:
- Call begin(), commit(), and rollback() directly, optionally configuring begin options such as a timeout. section 7.5.2
- Wrap work with requiringNew(), joiningExisting(), disallowingExisting(), or suspendingExisting() to select transaction semantics. section 7.5.2
- Configure wrapper timeouts and exception handlers that can choose whether certain failures still commit or cause rollback. section 7.5.2
One transaction per streamed car
A gRPC service can wrap each repository persist operation with QuarkusTransaction.requiringNew().run(() -> { carRepository.persist(car); });. This commits each car separately instead of keeping one transaction open for the entire stream, avoiding a long-lived transaction and possible timeout. section 7.2.2
Listing 7.25 Transaction wrappers provided by QuarkusTransaction
QuarkusTransaction.requiringNew().run(() -> { #1
// perform operation
});
var txResult = QuarkusTransaction.joiningExisting()
.timeout(30)
.exceptionHandler(throwable -> {
if (throwable instanceof AnyRecoverableException) {
return TransactionExceptionResult.COMMIT;
}
return TransactionExceptionResult.ROLLBACK;
}).call(() -> { #2
var result = // perform operation
return result;
});
The wrapper example combines transaction selection, a timeout, an exception handler, and a Callable result. section 7.5.2
