clj-r2dbc.impl.connection
Connection lifecycle internals for clj-r2dbc.
Manages connection acquisition and teardown using a RAII pattern:
Connection.close() is guaranteed on success, error, and Missionary
cancellation. When the caller supplies an existing Connection, RAII
ownership is skipped.
Provides:
ConnectableWithOpts - record pairing a raw connectable with default opts.
create-connection-factory* - URL delegation to ConnectionFactories.get.
acquire-connection - subscribes to ConnectionFactory.create(), returns task.
with-connection* - scoped connection sharing with RAII cleanup.
with-options* - wraps a connectable with default opts.
connection-metadata* - wraps ConnectionMetadata into a plain Clojure map.
Protocols extended here:
proto/Connectable - ConnectionFactory, Connection, ConnectableWithOpts.
proto/Describable - Connection.
This namespace is an implementation detail; do not use from application code.
acquire-connection
(acquire-connection db)
Acquire an R2DBC connection as a Missionary task.
When db is a ConnectionFactory, subscribes to ConnectionFactory.create()
and returns a task resolving to the Connection. The caller is responsible
for closing the connection (see with-connection* for RAII semantics).
When db is already a Connection, returns a task resolving to it directly;
lifecycle is owned by the enclosing scope.
ConnectableWithOpts is unwrapped before the instance check.
Args:
db - ConnectionFactory, Connection, or ConnectableWithOpts.
Returns a Missionary task resolving to a Connection.
Example:
(m/? (acquire-connection factory)) ;;=> Connection
(m/? (acquire-connection conn)) ;;=> conn (passed through)
create-connection-factory*
(create-connection-factory* {:keys [url]})
Create a ConnectionFactory from a configuration map.
The :url value is forwarded verbatim to ConnectionFactories.get(String).
Composite URLs (r2dbc:pool:..., r2dbc:proxy:...) are accepted.
Args:
opts - map containing :url - non-blank R2DBC URL string.
Returns a ConnectionFactory instance.
Example:
(create-connection-factory* {:url "r2dbc:h2:mem:///testdb"})resolve-connectable
(resolve-connectable db per-call-opts)
Unwrap a connectable into [raw-db merged-opts].
For ConnectableWithOpts, merges default options with per-call-opts (nil
values in per-call-opts don't clobber defaults). For raw
ConnectionFactory/Connection, returns [db per-call-opts] unchanged.
Args:
db - ConnectionFactory, Connection, or ConnectableWithOpts.
per-call-opts - options map from the caller.
with-connection*
(with-connection* db body-fn)
RAII-managed connection sharing scope.
Acquires a connection from db (ConnectionFactory or ConnectionPool) and
passes it to body-fn. The connection is guaranteed to be closed on success,
error, and Missionary cancellation - all three paths call Connection.close()
exactly once.
When db is already a Connection, passes it directly without RAII wrapping;
lifecycle is owned by the caller and Connection.close() is NOT called.
ConnectableWithOpts is unwrapped before the instance check.
Args:
db - ConnectionFactory, Connection, or ConnectableWithOpts.
body-fn - 1-arity fn [Connection] -> Missionary task.
Returns a Missionary task resolving to the body-fn result.
Example:
(with-connection* pool
(fn [conn]
(m/sp (m/? (execute conn "SELECT 1")))))with-options*
(with-options* db opts)
Wrap a connectable with default options.
Supports nesting: inner defaults override outer defaults.
Args:
db - ConnectionFactory, Connection, or ConnectableWithOpts.
opts - map of default options to attach.
Wrapped
protocol
members
-unwrap
(-unwrap this)
Return [raw-db default-opts-map].