对 SQL Server 的调用将忽​​略 pyodbc.connect 超时参数 [英] pyodbc.connect timeout argument is ignored for calls to SQL Server

查看:38
本文介绍了对 SQL Server 的调用将忽​​略 pyodbc.connect 超时参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Linux 上使用带有 FreeTDS 的 pyodbc 连接到 SQL Server 2005.我注意到我的连接的超时参数没有被我的查询所接受.

I am using pyodbc on Linux with FreeTDS to connect to SQL Server 2005. I have noticed that the timeout argument to my connection is not being honoured by my queries.

当我运行以下命令时,我希望在两个 cursor.execute 调用后都能看到超时错误.

When I run the following I would expect to see timeout errors after both cursor.execute calls.

import pyodbc
import time

connString = 'SERVER=dbserver;PORT=1433;DATABASE=db;UID=dbuser;PWD=dbpwd;' + \
    'DRIVER=FreeTDS'
cnxn = pyodbc.connect(connString , timeout=3)

cursor = cnxn.cursor()

t1  = time.time()
cursor.execute("SELECT MAX(Qty) FROM big_table WHERE ID<10000005")
print cursor.fetchone()
t2 = time.time()
print t2-t1

cursor.execute("WAITFOR DELAY '00:00:30'")
print 'OK'

相反,我得到了这个输出.表示第一个 db 查询耗时 7.5 秒,第二个调用耗时 30 秒,但未引发超时.

Instead I get this output. Indicating that the first db query is taking over 7.5 seconds and the second call is taking 30 seconds without throwing a timeout.

(808432.0, )
7.56196093559
OK

是否有更好的方法使用 pyodbc 和 SQL Server 强制查询超时?

Is there a better way to force a query timeout using pyodbc and SQL Server?

推荐答案

参考 pyodbc connection,有是两个独立的超时参数,Connection 类上的一个变量(设置查询超时)和 pyodbc.connect 的关键字参数(这个参数用于实际连接过程).基于此,您将在代码中设置连接过程的超时,而不是查询.

Refer pyodbc connection, there are two separate timeout parameters, a variable on the Connection class (this sets the timeout for queries) and a keyword param to pyodbc.connect (and this one for the actual connection process). Based on this you are setting the timeout for the connection process in your code and not for queries.

这篇关于对 SQL Server 的调用将忽​​略 pyodbc.connect 超时参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆