How to fix timeout error in connection using pyodbc in airflow

Xavier Langa 0 Reputation points
2024-11-23T06:00:19.38+00:00

Hi,

I am running an airflow DAG in docker, where I connect to sql server 2019 using a pyodbc connection function.

My connection function is as:

def get_db_connection():
    return pyodbc.connect(
        'DRIVER={ODBC Driver 17 for SQL Server};'
        'SERVER=xxx.xxx.x.xx\\XXX2024;'
        'DATABASE=somedatabase;'
        'UID=username;'
        'PWD=password;'
    )

When the connection exceeds 15 seconds, I get this error:

return pyodbc.connect( pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

even when setting:

connection.timeout = 30

Can someone help me?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,153 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LiHongMSFT-4306 29,191 Reputation points
    2024-11-25T03:00:13.19+00:00

    Hi @Xavier Langa

    it does not always fail. It only fails when the connection attempt exceeds 15 seconds.

    Maybe you could try connecting using other tools like SSMS to verify if the issue still exists.

    In other hand, you could check the SQL Server error logs which may provide more details.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.