connection¶
connection
¶
Connection pooling, transactions, and audit logging for ccr_metadata.
Public API
get_connection() -- context manager yielding a pooled mariadb.Connection transaction() -- context manager yielding an audit-logging cursor execute() -- one-shot query helper returning list[dict] init_pool() -- explicit pool configuration close_pool() -- shutdown / test teardown
Credentials are read from ~/.my.cnf by default. The section name and any
individual fields can be overridden via init_pool(...) keyword arguments.
The database name additionally honors the NOXDB_DATABASE env variable
(env var loses to an explicit init_pool(database=...) override).
SSH tunneling¶
The production database lives on a Galera cluster inside the LiSC network and
is only reachable by SSH-ing through the project VM at
ccr-lab.lisc.univie.ac.at. To connect from outside LiSC, supply SSH
parameters and init_pool() will open a local-port-forwarding tunnel before
creating the pool. The DB host/port you configure are interpreted as
the remote DB endpoint (i.e. the Galera cluster as seen from the VM).
SSH parameters (kwargs > NOXDB_SSH_* env vars > [noxdb-ssh] INI section):
ssh_host, ssh_port (default 22), ssh_user, ssh_password, ssh_pkey,
ssh_pkey_password.
If ssh_host is unset the tunnel is skipped and the driver connects
directly to host:port (useful when running on the VM itself).
Write statements (INSERT/UPDATE/DELETE/REPLACE) issued via execute() or via
the cursor yielded by transaction() are appended to an audit log at
~/.noxdb/audit.log (override with NOXDB_AUDIT_LOG).
init_pool
¶
init_pool(pool_size: int = DEFAULT_POOL_SIZE, *, config_path: str | Path | None = DEFAULT_CONFIG_PATH, section: str = DEFAULT_SECTION, host: str | None = None, port: int | None = None, user: str | None = None, password: str | None = None, database: str | None = None, ssh_host: str | None = None, ssh_port: int | None = None, ssh_user: str | None = None, ssh_password: str | None = None, ssh_pkey: str | None = None, ssh_pkey_password: str | None = None) -> None
Create the connection pool.
When ssh_host resolves to a non-empty value (via kwarg,
NOXDB_SSH_HOST, or the [noxdb-ssh] config section), an SSH
tunnel is opened to that host and the pool connects through it;
the configured DB host:port is the tunnel's remote bind target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pool_size
|
int
|
Number of pooled connections. |
DEFAULT_POOL_SIZE
|
config_path
|
str | Path | None
|
Path to the MariaDB-style config file. Pass
|
DEFAULT_CONFIG_PATH
|
section
|
str
|
INI section to read DB credentials from. |
DEFAULT_SECTION
|
host
|
str | None
|
DB host override. Wins over the config file. |
None
|
port
|
int | None
|
DB port override. |
None
|
user
|
str | None
|
DB user override. |
None
|
password
|
str | None
|
DB password override. |
None
|
database
|
str | None
|
DB name override. Also honours the |
None
|
ssh_host
|
str | None
|
SSH jump host. When non-empty, a tunnel is opened. |
None
|
ssh_port
|
int | None
|
SSH port (default 22). |
None
|
ssh_user
|
str | None
|
SSH username. |
None
|
ssh_password
|
str | None
|
SSH password (used if |
None
|
ssh_pkey
|
str | None
|
Path to private key. Tried first for auth. |
None
|
ssh_pkey_password
|
str | None
|
Passphrase for |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the pool is already initialized (call
|
FileNotFoundError
|
If |
Source code in src/noxdb/connection.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | |
close_pool
¶
Close the pool, tear down the SSH tunnel, and release audit handlers.
Safe to call when no pool exists. Used by test teardown and at
shutdown. After this returns,
init_pool can be called
again.
Source code in src/noxdb/connection.py
get_connection
¶
Yield a pooled connection.
The connection commits on normal exit of the with block and
rolls back on any exception. The pool is initialized lazily on
first call with default settings. Server-side autocommit=0 is
re-asserted on every checkout to defend against pool-reset drift.
Yields:
| Type | Description |
|---|---|
Connection
|
A |
Connection
|
the pool (not destroyed) when the |
Source code in src/noxdb/connection.py
transaction
¶
Yield an audit-logging cursor. All statements share one transaction.
Commit and rollback are inherited from
get_connection: if the
with block exits normally everything commits; if any statement
raises, everything rolls back atomically.
Yields:
| Type | Description |
|---|---|
_LoggingCursor
|
A |
_LoggingCursor
|
(INSERT/UPDATE/DELETE/REPLACE) to |
_LoggingCursor
|
(override via |
Source code in src/noxdb/connection.py
execute
¶
Run one query and return rows as a list of dicts.
Each call uses its own pooled connection and its own transaction;
for multi-statement atomicity use
transaction instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL statement, optionally with |
required |
params
|
Any
|
Bind parameters. |
None
|
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
For SELECT, one |
list[dict[str, Any]]
|
value). For INSERT/UPDATE/DELETE/REPLACE, an empty list (and |
list[dict[str, Any]]
|
the statement is audit-logged). |