sql error: (’08S01′, ‘[08S01] [Microsoft][ODBC Driver 13 for SQL Server]Communication link failure (0) (SQLExecDirectW)’)

在使用pyodbc 從 python 連到 MSSQL 時,當有一個 thread 固定在 polling database 裡的值,但每次連database 不是重新建立 connection 而是沿用 global 的 connection,在重啟 sql server 時可以遇到這個 Error.

('08S01', '[08S01] [Microsoft][ODBC Driver 13 for SQL Server]Communication link failure (0) (SQLExecDirectW)')

 

如果是去取得 table 物件,會遇到這一個 Error:

 ('08S01', '[08S01] [Microsoft][ODBC Driver 13 for SQL Server]Communication link failure (0) (SQLTables)'

 

 

python 2.7 – How to re-establish connection after communication link failure with pyodbc?

Per my experience, I think may be you can try to use the code below to implement the retry logic.

import time

retry_flag = True
retry_count = 0
while retry_flag and retry_count < 5:
  try:
    cursor.execute(query, [args['type'], args['id']])
    retry_flag = False
  except:
    print "Retry after 1 sec"
    retry_count = retry_count + 1
    time.sleep(1)

 

 

相關文章:

Strange bug when upgrading to django 1.5.2 and Native Client 11.0 #2
https://github.com/michiya/django-pyodbc-azure/issues/2

Connection hanging due to MSSQL lock on session updates #4
https://github.com/michiya/django-pyodbc-azure/issues/4

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *