I noticed the number of connections to my database can grow dramatically fast, and then drop. The problem is sometimes it doesn’t drop itself, and I don’t understand the overall behaviour.
Trying to solve the problem, I’m trying to close the session in my Python code:
import db # It's the SQLAlchemy object
engine_container = None
def init(app):
global engine_container
with app.app_context():
if engine_container is None:
engine_container = db.get_engine()
def cleanup_session(session):
global engine_container
session.close()
engine_container.dispose()
return
def long_function():
db_session = db.session
try:
# Long functioning code
except:
# Process exceptions
pass
finally:
if db_session is not None:
cleanup_session(db_session)
As you can see from the graph above, most of the connections are idle. In pgAdmin, when trying to see what’s behind them, they just show this and not very informative.
Is this behaviour normal? Why, sometimes, the number of connection gains the threshold and database is staying unresponsive for some period of time (today it was ~45 minutes!!! what is very bad). Any suggestions?
7