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
| Attribute | Type | Description |
|---|---|---|
| settings_name | string = null | The name of the Django settings attribute used to retrieve connection configurations when no settings are provided. |
| exception_class | exception class = ConnectionDoesNotExist | The exception type raised when a requested connection alias is not found within the configured settings. |
| thread_critical | boolean = false | A boolean flag that determines whether the underlying local storage for connections should be thread-critical. |
Constructor
Signature
def BaseConnectionHandler(
settings: dict = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| settings | dict = None | A 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
| Type | Description |
|---|---|
dict | The 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
| Name | Type | Description |
|---|---|---|
| settings | dict = null | The initial settings dictionary or None to trigger a lookup from Django settings. |
Returns
| Type | Description |
|---|---|
dict | The 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
| Name | Type | Description |
|---|---|---|
| alias | string | The unique identifier for the connection configuration to be instantiated. |
Returns
| Type | Description |
|---|---|
object | A 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
| Name | Type | Description |
|---|---|---|
| initialized_only | boolean = false | If True, only returns connections that are already active in the current thread. |
Returns
| Type | Description |
|---|---|
list | A 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
| Type | Description |
|---|---|
null | null |