Skip to main content

BaseConnectionHandler

This class provides a base implementation for managing a collection of thread-local network or database connections based on provided settings. It supports dictionary-style access to connections, lazy initialization through subclass-defined creation logic, and utility methods for iterating over or closing all active connections.

Attributes

AttributeTypeDescription
settings_namestring = nullThe name of the Django settings attribute used to retrieve connection configurations when no settings are provided.
exception_classexception class = ConnectionDoesNotExistThe exception type raised when a requested connection alias is not found within the configured settings.
thread_criticalboolean = falseA boolean flag that determines whether the underlying local storage for connections should be thread-critical.

Constructor

Signature

def BaseConnectionHandler(
settings: dict = None
) - > null

Parameters

NameTypeDescription
settingsdict = NoneA dictionary of connection settings. If None, settings will be retrieved from Django settings during first access.

Methods


settings()

@classmethod
def settings() - > dict

Provides access to the connection settings, ensuring they are configured before use.

Returns

TypeDescription
dictThe dictionary of connection configurations.

configure_settings()

@classmethod
def configure_settings(
settings: dict = null
) - > dict

Initializes the settings dictionary, defaulting to the global Django settings if no specific configuration is provided.

Parameters

NameTypeDescription
settingsdict = nullThe initial settings dictionary or None to trigger a lookup from Django settings.

Returns

TypeDescription
dictThe processed settings dictionary to be used for connection management.

create_connection()

@classmethod
def create_connection(
alias: string
) - > object

Abstract method that must be implemented by subclasses to define how a specific connection instance is instantiated.

Parameters

NameTypeDescription
aliasstringThe unique identifier for the connection configuration to be instantiated.

Returns

TypeDescription
objectA new connection instance corresponding to the provided alias.

all()

@classmethod
def all(
initialized_only: boolean = false
) - > list

Returns a list of connection instances, optionally filtering to include only those that have already been initialized.

Parameters

NameTypeDescription
initialized_onlyboolean = falseIf True, only returns connections that are already active in the current thread.

Returns

TypeDescription
listA list of connection objects.

close_all()

@classmethod
def close_all() - > null

Iterates through all currently initialized connections and invokes their close method to release resources.

Returns

TypeDescription
nullnull