connection pool - SQLAlchemy & Flask

那年仲夏 提交于 2019-12-11 06:29:47

问题


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)


回答1:


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)


来源:https://stackoverflow.com/questions/53891617/connection-pool-sqlalchemy-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!