Python 如何关闭 SQLAlchemy 会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21738944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close a SQLAlchemy session?
提问by fedorqui 'SO stop harming'
Following what we commented in How to close sqlalchemy connection in MySQL, I am checking the connections that SQLAlchemy creates into my database and I cannot manage to close them without exiting from Python.
按照我们在如何关闭 MySQL 中的 sqlalchemy 连接中的评论,我正在检查 Sqlalchemy 在我的数据库中创建的连接,如果不退出 Python,我无法关闭它们。
If I run this code in a python console, it keeps the session opened until I exit from python:
如果我在 python 控制台中运行此代码,它会保持会话打开,直到我退出 python:
from sqlalchemy.orm import sessionmaker
from models import OneTable, get_engine
engine = get_engine(database="mydb")
session = sessionmaker(bind=engine)()
results = session.query(OneTable.company_name).all()
# some work with the data #
session.close()
and the only workaround I found to close it is to call engine.dispose()at the end.
我发现关闭它的唯一解决方法是engine.dispose()最后调用。
As per the comments in the link I gave above, my question are now:
根据我上面给出的链接中的评论,我现在的问题是:
- Why is
engine.dispose()necessary to close sessions? - Doesn't
session.close()suffice?
- 为什么
engine.dispose()需要关闭会话? - 还
session.close()不够?
采纳答案by zzzeek
There's a central confusion here over the word "session". I'm not sure here, but it appears like you may be confusing the SQLAlchemy Sessionwith a MySQL @@session, which refers to the scope of when you first make a connection to MySQL and when you disconnect.
这里有一个关于“会话”这个词的核心混淆。我在这里不确定,但看起来您可能将SQLAlchemy Session与MySQL @@session混淆,后者指的是您第一次与 MySQL 建立连接以及断开连接时的范围。
These two concepts are not the same. A SQLAlchemy Session generally represents the scope of one or more transactions, upon a particular database connection.
这两个概念并不相同。一个 SQLAlchemy Session 通常代表一个或多个事务的范围,在特定的数据库连接上。
Therefore, the answer to your question as literally asked, is to call session.close(), that is, "how to properly close a SQLAlchemy session".
因此,您的问题的答案是按字面意思进行调用session.close(),即“如何正确关闭 SQLAlchemy 会话”。
However, the rest of your question indicates you'd like some functionality whereby when a particular Sessionis closed, you'd like the actual DBAPI connection to be closed as well.
但是,您的问题的其余部分表明您想要一些功能,即在Session关闭特定功能时,您也希望关闭实际的 DBAPI 连接。
What this basically means is that you wish to disable connection pooling. Which as other answers mention, easy enough, use NullPool.
这基本上意味着您希望禁用连接池。正如其他答案提到的那样,很容易,使用 NullPool。
回答by julivico
session.close()will give the connection back to the connection pool of Engine and doesn't close the connection.
session.close()会将连接返回给 Engine 的连接池,并且不会关闭连接。
engine.dispose()will close all connections of the connection pool.
engine.dispose()将关闭连接池的所有连接。
Engine will not use connection pool if you set poolclass=NullPool. So the connection (SQLAlchemy session) will close directly after session.close().
如果你设置了,引擎将不会使用连接池poolclass=NullPool。所以连接(SQLAlchemy 会话)将在session.close().

