Connecting MSSQL in python using pyodbc and getting random pyodbc Operational error

Viewed 479

I have a python code which connects to MSSQL database using pyodbc. I fetch the data from MSSQL and do some modifications on it. The code runs on a Linux server. The code was working fine initially but after few months I have been getting completely random OperationalError while connecting.

pyodbc.OperationalError: ('08S01', '[08S01] [Microsoft][ODBC Driver 17 for SQL Server]TCP Provider: Error code 0x274C (10060) (SQLExecDirectW)')

The errors are completely random. I use stored procedure to fetch the data and they run fine on my SQL Server Management Studio. I use SQL Server 2017, python 3.5.2 and pyodbc 4.0.30. I use following statement to connect to the database.

dbconnection = pyodbc.connect("Driver={ODBC Driver 17 for SQL Server};Server="+Server+";Database="+Database+";UID="+UserID+";PWD="+Password, autocommit=True)

I have tried using the reconnect code to tackle the problem. But problem persists, the code just fails five times always.

**

retry_flag = True
retry_count = 0
while retry_flag and retry_count < 5:
  try:
      cursor = dbconnection.cursor()
      cursor.execute("EXEC sp")
      countyList = cursor.fetchall()
      retry_flag = False
  except:
      print ("Retry after 60 sec")
      retry_count = retry_count + 1
      time.sleep(60)

**

I can provide more information if that will help answer this question.

0 Answers
Related