【问题标题】:context manager sqlalchemy close connection on fetchone上下文管理器 sqlalchemy 在 fetchone 上关闭连接
【发布时间】:2018-03-09 05:59:47
【问题描述】:

我有一个特定的用例:

  1. 我正在使用 SQLAlchemy Core
  2. 我只想从ResultsProxy.fetchone()
  3. 上下文管理器会自动关闭我的连接还是我必须手动关闭它?:

::

with engine.connect() as sqla_conn:
    a_result_dict = dict(sqla_conn.execute(a_sqlalchemy_selectable).fetchone())

# will sqla_conn be closed after __exit__? 
# or will it be kept open because the cursor may still have available rows to fetch?

【问题讨论】:

    标签: python sqlalchemy contextmanager


    【解决方案1】:

    一旦程序超出'with'范围,上下文管理器将关闭连接,更多信息请参阅link

    【讨论】: