我正在执行以下代码。该代码故意包含一个比
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=}")
我很期待
相反
10
分钟可以以某种方式强制预期的行为吗?
我遇到了同样的问题。事实证明这是 ODBC 驱动程序中的一个错误,已在版本 18.4.1.1 中修复。 (“修复超时断开连接延迟10分钟的问题”)