I would like to know what is proper way to close all mysql connections in sqlalchemy
.
For the context, it is a Flask application and all the views share the same session
object.
engine = create_engine("mysql+pymysql://root:root@127.0.0.1/my_database")
make_session = sessionmaker(bind=engine, autocommit=False)
session = ScopedSession(make_session)()
And when the app is teared down, the session
is closed and engine
is disposed
session.close()
engine.dispose()
But according to database log, I still have a lot of errors like [Warning] Aborted connection 940 to db: 'master' user: 'root' host: '172.19.0.7' (Got an error reading communication packets)
.
I have tried some solutions, including calling gc.collect()
and engine.pool.dispose()
but without success ...
I suspect there are still some connections opened by the engine behind the scene and they need to be closed. Is there anyway to list all the sessions/connections opened by the engine?
After spending a lot of time on this, any advice/help/pointer will be much appreciated! Thanks.
P.S: the dispose
and close
calls are inspired from How to close sqlalchemy connection in MySQL. Btw, what is a 'checked out' connection?