Skip to main content

SessionManager

Manage SQLAlchemy sessions.

Attributes

AttributeTypeDescription
_enginesdict = {}A dictionary mapping database URIs to their respective SQLAlchemy engine instances for reuse across the application.
_sessionsdict = {}A dictionary mapping database URIs to sessionmaker factories used to generate new database sessions.
forkedboolean = falseA boolean flag indicating whether the process has been forked, used to determine if engines and sessions should be cached or recreated.
preparedboolean = falseA boolean flag that tracks whether the database models and metadata have been successfully initialized on the target engine.
engine_callbackcallable = nullAn optional callable that is executed whenever a new SQLAlchemy engine is created, typically used for engine configuration or logging.

Constructor

Signature

def SessionManager(
engine_callback: callable = None
) - > null

Parameters

NameTypeDescription
engine_callbackcallable = NoneAn optional callback function to be executed whenever a new SQLAlchemy engine is created.

Methods


get_engine()

@classmethod
def get_engine(
dburi: string,
**kwargs: dict
) - > Engine

Retrieves or creates a SQLAlchemy engine for the specified database URI, using connection pooling if forked or a NullPool otherwise.

Parameters

NameTypeDescription
dburistringThe connection string used to identify and connect to the target database.
**kwargsdictAdditional configuration arguments passed to the SQLAlchemy create_engine call.

Returns

TypeDescription
EngineThe SQLAlchemy engine instance configured for the provided database URI.

create_session()

@classmethod
def create_session(
dburi: string,
short_lived_sessions: boolean,
**kwargs: dict
) - > tuple

Initializes a session factory and its associated engine for a given database URI.

Parameters

NameTypeDescription
dburistringThe connection string for the database where the session will be bound.
short_lived_sessionsbooleanIf true, prevents the session factory from being cached in the internal registry.
**kwargsdictConfiguration parameters used when initializing the database engine.

Returns

TypeDescription
tupleA tuple containing the SQLAlchemy engine and the sessionmaker factory.

invalidate()

@classmethod
def invalidate(
dburi: string
)

Dispose cached engine/session state for a database URI.

Parameters

NameTypeDescription
dburistringThe database connection string whose associated engine and session factory should be cleared and disposed.

prepare_models()

@classmethod
def prepare_models(
engine: Engine
)

Ensures all database tables defined in the model metadata exist, implementing a retry mechanism to handle race conditions during creation.

Parameters

NameTypeDescription
engineEngineThe SQLAlchemy engine used to execute the DDL commands for table creation.

session_factory()

@classmethod
def session_factory(
dburi: string,
**kwargs: dict
) - > Session

Creates a new database session after ensuring that the necessary database models have been prepared.

Parameters

NameTypeDescription
dburistringThe connection string for the database to session into.
**kwargsdictConfiguration options passed through to the engine and session creation logic.

Returns

TypeDescription
SessionA new SQLAlchemy Session instance ready for database operations.