Reputation: 39
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 -
So in docs it is written that by default SQLAlchemy offers connection pooling with defaults, like max_pool_size = 5, max_overflow = 10. So my question is do i need to pass the parameter like this = cnx = create_engine('connection string', pool=queuepool) to envoke that default behavior or simple create_enine('connection string') will do that?
Do we have any class\function in flask package to do connection polling?
can anyone share any code\project link which has this setup?(some practical scenario will be helpful not any sample example)
Upvotes: 2
Views: 3219
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