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
Where connections come from. Not closed by close; whoever built the pool owns it.
Functions
Runs block with a transaction open, committing when it returns and rolling back when it throws.