Skip to main content

Transactors

A Transactor manages database connections and transactions. It provides a clean API for executing database operations with automatic connection and transaction lifecycle management.

Setting Up a Transactor

// The Transactor manages connections and transactions
// You choose the strategy — it handles the lifecycle
var tx = connectionSource.transactor(Transactor.defaultStrategy());

// Everything inside runs in one transaction: begin, commit, close
List<ProductRow> products = tx.execute(conn ->
Fragment.of("SELECT * FROM product WHERE price > ?")
.param(PgTypes.numeric, minPrice)
.query(ProductRow.rowParser.list())
.runUnchecked(conn)
);

Built-in Strategies

StrategyDescription
Transactor.defaultStrategy()begin, commit, close
Transactor.autoCommitStrategy()no transaction, just close
Transactor.rollbackOnErrorStrategy()begin, commit on success, rollback on error, close
Transactor.testStrategy()begin, rollback, close (for tests)

Custom Strategies

Define your own with explicit hooks:

new Transactor.Strategy(
conn -> conn.setAutoCommit(false), // before
Connection::commit, // after (success)
throwable -> { /* handle error */ }, // oops
Connection::close // always (finally)
);