Skip to main content

Module pool

Module pool 

Source
Expand description

Database connections backed by the model config.

Besides the one-shot connect/connect_with_url helpers, this module holds a process-global connection installed once at application boot (init) and read from request handlers via pool. Controllers reach it through Context::db().

Functions§

connect
Connects to the database named by the current environment’s config/<env>.yml database.url, honouring its logger.sql toggle and any database.pool / database.connect_timeout knobs.
connect_with_url
Connects using an explicit database URL, bypassing the config file. SQL statement logging stays enabled (the default).
init
Connects (via connect) and installs the process-global pool, returning a reference to it. Idempotent: if already initialised, the existing connection is returned and the new one discarded. Call once at server boot.
pool
Returns the global connection, panicking if init/set_pool was never called. Use from request handlers where boot is guaranteed to have run.
pool_options
Builds ConnectOptions with SQL logging plus optional pool tuning: max_connections (config database.pool) and connect_timeout in seconds (config database.connect_timeout).
set_pool
Installs an already-open connection as the global pool (e.g. in tests). Returns Err with the connection back if one was already installed.
test_lock
A process-global lock for serializing tests that share the global pool.
try_pool
Returns the global connection if installed, else None.