SQL engines#
Module: restalchemy.storage.sql.engines
This module contains the engine factory and concrete SQL engine implementations for MySQL and PostgreSQL.
AbstractEngine#
AbstractEngine defines the common behavior for all SQL engines:
- Parses the database URL.
- Exposes database name, host, port, username and password.
- Holds the SQL dialect (
mysql.MySQLDialectorpgsql.PgSQLDialect). - Provides a
session_manager()context manager.
Key properties and methods:
URL_SCHEMA(abstract): expected URL schema, e.g."mysql","postgresql".DEFAULT_PORT(abstract): port used if not specified in URL.db_name,db_username,db_password,db_host,db_port.dialect: the SQL dialect object.query_cache: whether session-level query cache is enabled.get_connection(): obtain a connection (implemented by subclasses).get_session(): obtain a session object (implemented by subclasses).session_manager(session=None): context manager that manages commit/rollback and closing the session.get_session_storage(): returns the session storage (SessionThreadStorage).
Example:
from restalchemy.storage.sql import engines
engine = engines.engine_factory.get_engine()
print(engine.db_name)
PostgreSQL engine#
PgSQLEngine#
URL_SCHEMA = "postgresql".DEFAULT_PORTis taken fromrestalchemy.common.constants.RA_POSTGRESQL_DB_PORT.- Uses
psycopg_pool.ConnectionPoolfor connections. - Dialect:
pgsql.PgSQLDialect(). - Session type:
sessions.PgSQLSession.
Constructor:
db_url: PostgreSQL connection URL.config: passed topsycopg_pool.ConnectionPool.query_cache: enables query caching.
Methods:
get_session(): returnsPgSQLSession(engine=self).get_connection(): gets a connection from the pool.close_connection(conn): returns the connection back to the pool.
The engine is created internally by EngineFactory.
Connection timeouts#
register_postgresql_db_opts() registers connection, server, and TCP timeout
options. Durations are configured in seconds. An omitted option keeps the
corresponding libpq, PostgreSQL, or operating-system setting. An explicit 0
is passed to the driver; for PostgreSQL server timeouts, it disables the
timeout.
connection_connect_timeout: time allowed to establish a connection.connection_statement_timeout: maximum statement execution time.connection_transaction_timeout: maximum transaction duration; requires PostgreSQL 17 or newer.connection_idle_in_transaction_session_timeout: maximum time a session may remain idle in a transaction.connection_tcp_user_timeout: maximum time transmitted data may remain unacknowledged.connection_keepalives_idle,connection_keepalives_interval, andconnection_keepalives_count: TCP keepalive detection parameters.
For example, the following configuration keeps statement and idle-transaction waits below four minutes and bounds transactions and unacknowledged TCP data at five minutes:
[db]
connection_connect_timeout = 30
connection_statement_timeout = 240
connection_transaction_timeout = 300
connection_idle_in_transaction_session_timeout = 240
connection_tcp_user_timeout = 300
connection_keepalives_idle = 60
connection_keepalives_interval = 30
connection_keepalives_count = 5
MySQL engine#
MySQLEngine#
URL_SCHEMA = "mysql".DEFAULT_PORTis taken fromRA_MYSQL_DB_PORT.- Uses
mysql.connector.pooling.MySQLConnectionPool. - Dialect:
mysql.MySQLDialect(). - Session type:
sessions.MySQLSession.
Constructor:
db_url: MySQL connection URL.config: pool configuration.query_cache: enables query caching.
Methods:
get_connection(): returns a connection from the pool.get_session(): returnsMySQLSession(engine=self).
EngineFactory and engine_factory#
EngineFactory#
A singleton responsible for configuring and storing engine instances.
Important methods:
configure_factory(db_url, config=None, query_cache=False, name="default")- Creates an engine instance based on
db_urland stores it undername. - Infers engine class from URL schema ("mysql", "postgresql").
configure_postgresql_factory(conf, section, name)- Helper for configuring PostgreSQL from a config object.
configure_mysql_factory(conf, section, name)- Helper for configuring MySQL from a config object.
get_engine(name="default")- Returns the configured engine instance.
destroy_engine(name="default")/destroy_all_engines()- Remove engines from the factory.
At module level:
Most applications use this singleton:
from restalchemy.storage.sql import engines
engines.engine_factory.configure_factory(db_url="mysql://...")
engine = engines.engine_factory.get_engine()
DBConnectionUrl#
DBConnectionUrl is a small helper that parses a DB URL and provides a censored string representation.
- Stores the parsed URL.
urlproperty returns the full URL string.__repr__hides the password by replacing it with:<censored>@.
This is mainly useful for logging and debugging.