Search code examples
pythonsqlalchemyconnection-poolingpsycopg2

Create a sqlalchemy engine using an existing psycopg2 connection pool


I'm adding a new ORM class using sqlalchemy's declarative mapping system. My codebase has an existing psycopg2 connection pool, which I want to reuse - I don't want code using my orm classes to have its own pool. There's a lot of existing code which directly calls get_conn on the psycopg2 pool, so I don't want to just replace it either.

I'm having a problem constructing the engine to connect with.

pool_config = {...}
POOL = psycopg2.pool.ThreadedConnectionPool(0, 32, **pool_config)

[...]

engine = sqlalchemy.create_engine('postgresql://', pool=POOL)
Session = sqlalchemy.orm.sessionmaker(bind=engine)
...

The problem is with my call to create_engine;

  File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 362, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 159, in create
    event.listen(pool, 'first_connect', on_connect)
  File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/event/api.py", line 63, in listen
    _event_key(target, identifier, fn).listen(*args, **kw)
  File "/home/ubuntu/environment/local/lib/python2.7/site-packages/sqlalchemy/event/registry.py", line 190, in listen
    dispatch_descriptor = getattr(target.dispatch, identifier)
AttributeError: 'ThreadedConnectionPool' object has no attribute 'dispatch'

Is it possible to use my existing pool in this way, or do I need to make a separate connection pool to be used by these classes?


Solution

  • You can use a custom connection function:

    create_engine('postgresql+psycopg2://', creator=POOL.getconn)