Sudhanshu Ojha
Sudhanshu Ojha

Reputation: 39

connection pool - SQLAlchemy & Flask

I have a situation. I am having a code which uses SQLAlchemy for database activities. And have application made in flask, flask_restful to access the db codes. Now while testing, through unittest, i am facing some issues. And i m sure it is because Flask keeps the connection open after a request completes. So i want to implement connection pooling in my codes to check.I went through the connection pooling official docs of SQLAlchemy. And here are my questios -

Upvotes: 2

Views: 3219

Answers (1)

PaulieTree
PaulieTree

Reputation: 643

SQLAlchemy does connection pooling by default, which means it will do that behaviour without specifying it, create_engine('connection_string') will be enough.

You can choose a specific pooling class if you like: https://docs.sqlalchemy.org/en/13/core/pooling.html#switching-pool-implementations

That can be used to disable the pooling entirely with the NullPool.

From SQLAlchemy docs:

from sqlalchemy.pool import NullPool
engine = create_engine(
          'postgresql+psycopg2://scott:tiger@localhost/test',
          poolclass=NullPool)

Upvotes: 1

Related Questions