python - 在 Ubuntu 上使用 Python 的 Azure WebApp 中的连接泄漏

标签 python azure sqlalchemy azure-web-app-service ubuntu-18.04

我有一个 Flask 应用程序在 Azure 上的 Ubuntu WebApp 上运行。每天早上我对应用程序的查询都会失败,并出现以下错误:

sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('08S01', '[08S01] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x68 (104) (SQLExecDirectW)')

我正在使用 SQLAlchemy ORM 查询我的 Azure SQL Server 实例。我认为我的连接由于以下原因而变得陈旧。

  1. 每天早上没人使用该应用程序时都会发生这种情况
  2. 经过 X 次失败的返回后,它开始工作,直到第二天早上。

然而,让事情变得更奇怪的是,当我检查 sys.dm_exec_sessions 时在 sql 服务器上,它没有显示任何事件连接(除了我正在执行检查的连接之外)。

此外,当我在本地运行 dockerized 应用程序并连接到数据库时,我没有收到此类错误。

如果有人遇到类似的问题,我希望获得一些见解,或者至少提供有关深入研究的建议。

https://azure.github.io/AppService/2018/03/01/Deep-Dive-into-TCP-Connections-in-App-Service-Diagnostics.html

此链接对我有帮助,但该解决方案仅适用于 Windows 应用程序,不适用于 Linux。

最佳答案

在@snakecharmerb的帮助下:

该应用程序实际上持有死连接池,将 pool_recycle 设置为更长的时间可以解决该问题。

engine = create_engine(
        key, echo=False, future=True,
        echo_pool=True,
        #  connection pool will log informational output such as when connections are invalidated.
        pool_recycle=3600
        #  causes the pool to recycle connections after the given number of seconds has passed.
    )

关于python - 在 Ubuntu 上使用 Python 的 Azure WebApp 中的连接泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70776063/

相关文章:

python - 使用 PyCrypto 使用 RSA 公钥解密消息

c# - Azure 不支持外语文件格式名称下载

azure - 如何更改Azure虚拟机中的时区?

django - 使用代理连接到django中的snowflake

python - 如何使用 alembic 将 MySQL 表转换为 utf8 字符集?

python - SQLAlchemy 自省(introspection)

python - 将元组更改为字典,它如何正确设置键值?

python - Counter() 和most_common

Python 包结构,setup.py 用于运行单元测试

python method() 正好接受 1 个参数(给定 2 个)