Good way or best practices for re-using existing SQLAlchemy engine (connection pool)
benhoyt opened this issue · 3 comments
We're using beaker's SQLAlchemy database-backed session support, but our web application also sets up its own SQLAlchemy engine instance. However, we'd like to be able to use a single, shared SQLAlchemy engine instance so that we can have a single SQLAlchemy connection pool, instead of two connection pools (and double the number of PostgreSQL connections as a result).
In ext/database.py it uses the DatabaseNamespaceManager.metadatas
dictionary to cache engine instanes, so it looks like we could do something like this to get what we want:
DatabaseNamespaceManager.metadatas[url + table_name] = our_existing_sqla_engine
Is this reasonable? It feels a bit hackish. Is there a better way to use a shared engine? If not, could we add a configuration option for an "engine-fetching function" to allow us to return our existing engine instance?
Any updates on this? I would love to integrate this into my webapp too (my web host limits the number of connections)!
There is currently no "clean" way to pass an existing engine to beaker in ext:database
.
The expected way is by relying on ext:sqla
instead which actually uses an existing engine.
If anyone is willing to provide a patch to allow it I'll gladly review that, I think we should just get rid of ext:sqla
on long term and have a single "sqlalchemy" based extension.
Seems https://github.com/bbangert/beaker/pull/37/files aimed at solving this.