Transactions
Use jdbc-tx when Kaumei JDBC should own local JDBC transaction
boundaries.
The transaction manager works with any JdbcConnectionProvider and supplies
the active connection to generated JDBC implementations.
Add the dependency
Section titled “Add the dependency”Use the same Kaumei JDBC version as your other Kaumei dependencies:
<properties> <kaumei-jdbc.version>develop-SNAPSHOT</kaumei-jdbc.version></properties><dependencies> <dependency> <groupId>io.kaumei.jdbc</groupId> <artifactId>jdbc-tx</artifactId> <version>${kaumei-jdbc.version}</version> </dependency></dependencies>Create the transaction manager
Section titled “Create the transaction manager”Create the manager from the connection provider that normally obtains a fresh connection from your pool. Pass the manager itself to generated JDBC implementations so their operations reuse the active transaction connection:
JdbcConnectionProvider pool = dataSource::getConnection;KaumeiTxManager txManager = KaumeiTxManager.getInstance(pool);TradeRepository repository = new TradeRepository$Jdbc(txManager);Run a transaction
Section titled “Run a transaction”Use required for an operation that must run in a transaction:
txManager.required(KaumeiTxDefinition.DEFAULT, context -> { repository.adjustTotalValue(trade.customerId(), trade.value()); repository.insertTrade(trade);});Without an active transaction, required starts one.
With an active transaction, it joins that transaction.
Only the call that started the transaction completes it.
A normal return commits that transaction.
A RuntimeException or Error rolls it back by default, or marks a joined
transaction for rollback.
Checked exceptions commit by default unless the transaction definition selects
them for rollback.
Call context.setRollbackOnly() to force a rollback.
When the owning scope completes, it rolls back and reports an unchecked
transaction failure.
Programmatic and declarative use
Section titled “Programmatic and declarative use”Programmatic use places the transaction boundary directly around a callback and does not require a framework integration.
Declarative use places @KaumeiTx on an interface or method and requires an
integration that invokes the same KaumeiTxManager operations.
The annotation alone does not start a transaction.
A bare @KaumeiTx defaults to MANDATORY, so a method that intentionally owns
a boundary declares REQUIRED or REQUIRES_NEW explicitly.
See the Avaje Inject integration for a small declarative example using the same customer and trade update.
Exact rules
Section titled “Exact rules”- Transaction Manager defines contexts, connection access, and resource ownership.
- Transaction Semantics defines transaction types, completion, attributes, and rollback rules.
- Transaction API defines the programmatic callbacks and synchronisation API.
- Declarative Transactions
defines
@KaumeiTxand the runtime interface proxy.