Skip to content

Database connection

How the pool opens on `<PREFIX>_SEAORM__URL` — proved at boot, over verified TLS — and how it closes with the app.

SeaOrmModule opens one pool on <PREFIX>_SEAORM__URL, which every unit of work in the binary runs on. It is proved before anything is served, verifies the server’s certificate unless its URL turns TLS off, and closes after every shutdown hook.

The boot proves the pool before anything is served. What Postgres refuses on every attempt — the user and password, a database it does not have, TLS that fails — fails the boot at once, naming <PREFIX>_SEAORM__URL and the SQLSTATE. Anything else — a server not listening yet, still starting or full, a host whose name does not resolve yet — is tried again within <PREFIX>_SEAORM__CONNECT_TIMEOUT_SECS, each attempt said at warn on nest_rs::orm by host and port, never by the URL, which carries the password; past the budget, the boot fails as unreachable, naming what the last attempt met.

The URL is read once, at the start, its password with it. The certificate files its sslrootcert, sslcert and sslkey name are read each time the pool opens a connection, so a renewed one reaches every connection opened after it, while those already open keep the certificate they began with.

The authority is read as libpq reads it: the URL’s sslrootcert, then libpq’s PGSSLROOTCERT beside a URL naming none, each in place of the system’s store, and the system’s store where neither names one or one names libpq’s system. A server the file does not vouch for fails the boot, the refusal naming where the authority is named. sqlx keeps the public authorities it compiles in beside any of them. PGSSLCERT and PGSSLKEY present a client certificate beside a URL naming neither, as sslcert and sslkey do. A file any of them names that this process cannot read fails the boot at once, naming where it is set, rather than being tried again until the budget runs out.

The pool closes when the app ends, after every shutdown hook: each connection gets Postgres’s goodbye rather than a dropped socket, said before the close returns. Every unit of work has settled by then, so one still on its way back is waited for half a second at most; one a cut request left running a statement is not waited for — sqlx hands it back with no goodbye — so it falls with the process, and a warn counts it (the way down).