Connection to Azure database fails only at the first attempt in a series

Smail Kozarcanin 1 Reputation point
2022-05-25T09:38:14.053+00:00

I am running a very simple SQL-call through python on windows 10. This SQL-call is a part of a larger tool. When this tool is ran for the first time after, say 1 day, then the very first SQL-call that is connecting to an Azure database is failing with the error as shown in the attachment. If then the tool is ran immediately afterwards there are no issues and the connection and data extraction is successful. It is also very hard to provoke this error and therefore difficult to assess. I am not sure if it makes sense to attach the SQL connection, as it functions well. But, for easier reference it is shown below. "?" indicates hidden info.

from sqlalchemy import create_engine  
import urllib  
import pandas as pd  
  
database_dict = {'?': '?'}  
  
uids = ['?']  
pwds = ['?']  
  
connections = database_dict  
  
for i in range(len(database_dict)):  
  
    server = list(database_dict.values())[i]  
    database = list(database_dict.keys())[i]  
    uid = uids[i]  
    pwd = pwds[i]  
      
    params = urllib.parse.quote_plus("Driver={ODBC Driver 17 for SQL Server};"  
                                          f"Server={server};"  
                                          f"Database={database};"  
                                          f"uid={uid};"  
                                          f"pwd={pwd}")  
      
    con = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)  
      
    connections[database] = con  

205452-azure-error.jpg

Azure SQL Database
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Lorran Pedrosa Leite 5 Reputation points
    2024-04-05T12:23:51.48+00:00

    Good morning, I came across the same problem. I did the following, I created a loop of 3 connection attempts, with a brief sleep. If the 3 attempts fail, the process is not executed and an email is sent.

    I hope this tip helps anyone who needs it!

    1 person found this answer helpful.

  2. Pablo Gabriel Mederos Caballero 1 Reputation point
    2022-05-25T12:52:28.157+00:00

    This usually happens when the connection was too long without being used, so it exceeds the time defined by the server to wait for connections, and it is closed.
    Ideally, before using the connection, you should test it and if it's not active, create another one.

    0 comments No comments

  3. Oury Ba-MSFT 20,931 Reputation points Microsoft Employee Moderator
    2022-06-02T22:32:53.327+00:00

    Hi @Smail Kozarcanin Thank you for posting your query on Microsoft Q&A and for using Azure services.
    My understanding is that you are not able to connect to Azure SQL Database using Python. Please let me know if my understanding is not correct.
    Please try to use pyodbc

    import pyodbc
    server = 'your_server.database.windows.net'
    database = 'your_database'
    username = 'your_user'
    password = 'your_password'
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
    cursor = cnxn.cursor()

    Here is another example https://github.com/azure-samples/mssql-django-samples/tree/main/

    Please let me know if that was helpful.

    Do not forget to mark as accept answer if the reply was helpful as this will be Beneficial for other community members with the same question.

    Regards,
    Oury


  4. Aditi Jain 0 Reputation points
    2025-05-07T11:35:49.5266667+00:00

    Hi, you can try the following steps -

    1. Go to your database service in azure portal
    2. Go to Settings->Compute + Storage
    3. Scroll down and find Auto-pause delay
      Screenshot 2025-05-07 170453
    4. Disable it
    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.