Skip to main content

Strategies

A Transactor.Strategy defines hooks that wrap every execution. The built-in strategies cover common patterns — this page covers customization.

Hook Lifecycle

HookWhen it runs
onBeginBefore your code — typically setAutoCommit(false)
onSuccessAfter your code succeeds — typically commit
onFailureWhen an exception is thrown (catch) — receives the connection and the throwable
onCompleteIn all cases (finally) — typically close
listenerA QueryListener for observability (see Observability)

Custom Strategies

Build a strategy from scratch using replaceX methods — each one sets a single hook:

val customStrategy: Strategy =
Strategy.empty()
.replaceOnBegin { conn -> conn.autoCommit = false }
.replaceOnSuccess(Connection::commit)
.replaceOnFailure { _, _ -> /* handle error */ }
.replaceOnComplete(Connection::close)

Strategy Merging

Use mergeX methods to compose hooks — both the existing and new hook run in order. mergeListener composes listeners:

val base: Strategy = Transactor.defaultStrategy()
val withLogging: Strategy = base.mergeListener(logger)

The mergeListener convenience on Transactor creates a derived transactor with the listener merged into its strategy:

val txWithLogging: Transactor = tx.mergeListener(logger)