【发布时间】:2019-05-16 07:22:07
【问题描述】:
我在插入 SQL Server 时遇到了这个奇怪的信号量超时问题。下面是可以重现问题的简短 sn-p 代码。
运行相同代码库的同事都没有数据库存在问题。我们还有一个应用服务器,可以在几个小时内保持与数据库的连接而不会出现问题。在我的笔记本电脑上运行的不同之处在于:1)我有最新的戴尔 Precision 5530,这与我的同事不同; 2) 我使用的是 Anaconda/Python 3.7/pyodbc 4.0.26/Windows 10 的最新版本,而我的同事使用的是 python 3.5/3.6/Windows 7&Linux。谷歌搜索建议我尝试更新我尝试过的网络适配器驱动程序并且使用最新的驱动程序。
有人遇到过类似的问题吗?
[编辑 1] 感谢以下建议,我已更新到更新的驱动程序,但出现了不同的错误。
import time
import pyodbc
cxn = pyodbc.connect('DSN=db;DATABASE=db;Uid=user;Pwd=password', autocommit=True)
def insert(cxn):
ids = [(i, ) for i in range(100000)]
cur = cxn.cursor()
cur.fast_executemany = True
cur.execute('create table #tmp (unique_id int)')
# cur.commit()
print(1)
cur.executemany('insert into #tmp values (?)', ids)
# cur.commit()
print(2)
cur.execute('create unique index tmp_pk on #tmp (unique_id)')
# cur.commit()
print(3)
cur.execute('select * from #tmp')
print(4)
cur.execute('drop table #tmp')
for i in range(1000):
print('loop {} {}'.format(i, time.time()))
insert(cxn)
time.sleep(60)
在 ipython 中多次迭代后:
loop 6 1557992375.617378
1
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
<ipython-input-1-8ceea4a06017> in <module>
23 for i in range(1000):
24 print('loop {} {}'.format(i, time.time()))
---> 25 insert(cxn)
26 time.sleep(60)
<ipython-input-1-8ceea4a06017> in insert(cxn)
11 # cur.commit()
12 print(1)
---> 13 cur.executemany('insert into #tmp values (?)', ids)
14 # cur.commit()
15 print(2)
OperationalError: ('08S01', '[08S01] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.\r\n (10060) (SQLExecute); [08S01] [Microsoft][ODBC Driver 17 for SQL Server]Communication link failure (10060)')
错误是:
[08S01] [Microsoft][ODBC Driver 17 for SQL Server] TCP 提供程序:
连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者连接的主机没有响应,建立连接失败。
(10060) (SQL 执行); [08S01] [Microsoft][ODBC Driver 17 for SQL Server]通信链路故障 (10060)')
【问题讨论】:
-
您也使用了错误的驱动程序。您使用的是deprecated native client 而不是the ODBC driver。 Python examples in the docs 使用 ODBC 驱动程序
-
您可以在[这里]找到最新版本的 ODBC 驱动程序(当前为at v 17。当前版本为 17。连接字符串应如下所示:
'DRIVER={ODBC Driver 17 for SQL Server};SERVER=server;DATABASE=database;UID=username;PWD=password -
facepalm 我没有意识到这一点。刚刚安装了v17。会让你知道它是怎么回事。谢谢!
标签: sql-server pyodbc