为什么 pyodbc connection.close 在 Azure-sql-database 上出现超时错误后挂起 10 分钟?

问题描述 投票:0回答:1

我正在执行以下代码。该代码故意包含一个比

query timeout
更长的查询。

import pyodbc

TIMEOUT_SEC = 1
QUERY_DELAY_SEC = TIMEOUT_SEC + 1
DSN = ".."
QUERY = f"WAITFOR DELAY '00:00:{QUERY_DELAY_SEC:02d}';SELECT 1;"

conn = pyodbc.connect(DSN)
conn.timeout = TIMEOUT_SEC
cursor = conn.cursor()
try:
    result = cursor.execute(QUERY).fetchall()
    cursor.close()
except Exception as error:
    print(error)
finally:
    # if timeout exception happens this close gets a ~10 min. wait
    # if query succeed (e.g. with removed `WAITFOR DELAY`), close is instant 
    conn.close();

print(f"{result=}")

我很期待

  1. 引发超时错误
  2. 打印错误
  3. 退出脚本

相反

  1. 引发超时错误
  2. 打印错误
  3. 等待
    10
    分钟
  4. 退出脚本

可以以某种方式强制预期的行为吗?

python azure-sql-database pyodbc wait freeze
1个回答
0
投票

我遇到了同样的问题。事实证明这是 ODBC 驱动程序中的一个错误,已在版本 18.4.1.1 中修复。 (“修复超时断开连接延迟10分钟的问题”)

© www.soinside.com 2019 - 2024. All rights reserved.