Azure SQL -Datenbankverbindung Timeout von Azure Function (Python)Python

Python-Programme
Anonymous
 Azure SQL -Datenbankverbindung Timeout von Azure Function (Python)

Post by Anonymous »

Ich habe eine Azure -Funktion (Python -Laufzeit, Konsumplan), die eine Verbindung zu einer Azure -SQL -Datenbank herstellen muss. Ich verwende die Pyodbc -Bibliothek, um eine Verbindung herzustellen. Meistens funktioniert es einwandfrei, aber zeitweise erhalte ich Verbindungszeitüberschreitungsfehler. Der Fehler sieht ungefähr so ​​aus: < /p>

Code: Select all

pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 18 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
Ich habe versucht, den Parameter Timeout im pyodbc.connect () -Amruf zu erhöhen, es scheint jedoch die intermittierenden Zeitüberschreitungen nicht zu beheben. Es scheint unter schwererer Last häufiger zu passieren.

Code: Select all

import pyodbc
import os
import logging

def main(req: func.HttpRequest) -> func.HttpResponse:
try:
server = os.environ["SQL_SERVER"]
database = os.environ["SQL_DATABASE"]
connection_string = f"Driver={{ODBC Driver 18 for SQL Server}};Server=tcp:{server},1433;Database={database};Authentication=ActiveDirectoryMsi;"

# Attempt to connect (with a longer timeout, which doesn't fix the issue)
with pyodbc.connect(connection_string, timeout=60) as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT @@VERSION")
row = cursor.fetchone()
logging.info(f"SQL Version: {row[0]}")

return func.HttpResponse("Successfully connected to the database.", status_code=200)

except Exception as e:
logging. Error(f"Error connecting to database: {e}")
return func.HttpResponse("Failed to connect to the database.", status_code=500)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post