Skip to content

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.

Use the same Kaumei JDBC version as your other Kaumei dependencies:

pom.xml
<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 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);

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 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.