DefaultSessionProvider

The SessionProvider for an application that runs its own transactions, binding the session to the thread for as long as the outermost operation on it lasts.

A top-level operation borrows a session from dataSource and gives it back when it returns. That is as cheap as it sounds: the session is a handful of allocations over the connection, and giving it back costs no round trip unless it registered a LISTEN or left a transaction open.

Operations made from inside one another share that session, transaction or no transaction. Composing functions that each do a query is the ordinary way to use this client, and a provider that borrowed again per level would hold one connection per level of nesting - two connections for work that needs one, and a pool of N serving N / D callers at depth D. Sharing costs nothing, because a connection can only carry one exchange at a time anyway: the levels are sequential whether they share or not.

The one thing sharing does not survive is a level that begins while the one above it is still reading a result - a forEach* block, or a ResultConverter. That connection is busy, and the driver says so with InvalidOperationException(CONNECTION_BUSY) rather than quietly finding a second one. Code that genuinely needs a second connection in that position should take it deliberately, out of the DataSource.

Where something else owns the transaction - Spring, most obviously - this is the wrong provider; that one finds the session where the framework put it rather than on a ThreadLocal of its own.

Parameters

dataSource

Where connections come from. Not closed by close; whoever built the pool owns it.

Constructors

Link copied to clipboard
constructor(dataSource: DataSource)

Functions

Link copied to clipboard
open override fun close()

Releases what this provider holds of its own. Whatever supplies its connections is not its to close.

Link copied to clipboard
open override fun <T> execute(action: OctaviusSessionOperations.() -> T): T

Runs action on the session for the current context.

Link copied to clipboard
open override fun <T> transaction(definition: TransactionDefinition, block: () -> T): T

Runs block with a transaction open, committing when it returns and rolling back when it throws.