Separate DB connection creation and session state initialization
|Reported by:||Anssi Kääriäinen||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Currently when creating a new connection we create the connection and initialize it in one go (in _cursor()). Even if a backend splits getting a new connection and initializing the session variable this is not done consistently between backends.
The reason for this split is that this gives nice access point for external pooling implementations. A pool can wrap the backend and the methods "get_new_connection()" and "close()". Instead of actually creating new connections and closing the connection we can just get a connection from the pool and initialize the state of it. The real backend doesn't need to know anything about the pool wrapper.
The pool wrapper implementation would be something like this:
def get_new_connection(): if pooled_connections_available(): return connection from pool else: return super().get_new_connection() def init_connection(): do possible pool specific initialization super().init_connection() def close(): put the connection back to pool
I think this would allow connection pools for Django but with no need to implement them in core. In addition this should add readability of ._cursor() implementations.