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).
The tunnel is an OpenSSH ssh -N -L subprocess, so it needs the ssh
client on PATH and a login that works without a prompt: a key without a
passphrase, or one loaded into ssh-agent. Password login is not
supported for the tunnel.
SSH parameters (kwargs > NOXDB_SSH_* env vars > [noxdb-ssh] INI section):
ssh_host, ssh_port (default 22), ssh_user, ssh_pkey.
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_pkey: 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_pkey
|
str | None
|
Path to private key, passed to |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the pool is already initialized (call
|
FileNotFoundError
|
If |
Source code in src/noxdb/connection.py
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 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
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). |