SQL Server Connection for Python Flask Application

Ahad Anjum 60 Reputation points
2024-04-18T11:34:48.8833333+00:00

I have a python flask webapp and when the page is loading, connects to our SQL Server DB and saves certain tables as Panda data frames. This operation must happen as the page is loaded in. I am having issues connecting to my Server and database. Locally, everything runs completely fine, so I know this is a server-side issue. We have VNet integration set up for the web application and are experiencing weird and inconsistent errors.

The connection string that we are using is the following for SQLAlchemy:

user = os.environ[
password = os.environ[
hostName = os.environ[
port = os.environ[
db = os.environ[

# Establishing the database connection URI
database_uri = 
engine = create_engine(database_uri) 

We learnt recently that the hostname was causing us issues. What I was using locally was not working in the web app (shortened name for the host). We attempted two possible solutions. One was the numerical address which did not work, and the other was the full name.

The full name for the host name worked when I restarted the web app. However, it is no longer working.

This morning, I received the same SQL Server connection error which is: sqlalchemy.exc.OperationalError: (pyodbc.OperationalError) ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')

I then tried to switch the host name back to numerical address and it still was not working. When I switched it back to the full name and restarted the application it worked, and I was able to communicate for a couple messages before an error in my code (something related to state management which is not implemented yet as this app is in testing, and we only have one instance of it). However, when I reloaded the page again, I started to receive the same SQL connection errors again. I am puzzled as to why it worked twice when the host name variable was switched to the full name and the app was restarted and why in other scenarios it does not work at all. Currently the site and the host name environment variable are in the condition they were when the web app worked, only know the same error persists and I am not sure how to solve it. I can't check application logs either since the application fails to start due to the SQL Server Connection

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,737 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,901 questions
0 comments No comments
{count} votes

Accepted answer
  1. brtrach-MSFT 15,256 Reputation points Microsoft Employee
    2024-04-23T05:04:31.4+00:00

    @Ahad Anjum It seems you’re encountering a common issue with SQL Server connections in Azure, particularly with the OperationalError related to login timeouts. This error typically indicates that the connection to the SQL Server is taking too long to establish.

    My suggestions to start troubleshooting are:

    1. Verify the Connection String: Ensure that your connection string is correctly formatted and includes all necessary parameters. For Azure SQL Server, the connection string should specify the SQL driver name and other parameters like Encrypt, TrustServerCertificate, and Connection Timeout. Here’s an example of how to construct the connection string:
         import urllib.parse
         from sqlalchemy import create_engine
         params = urllib.parse.quote_plus(
             "Driver={ODBC Driver 17 for SQL Server};"
             "Server=tcp:your_server_name.database.windows.net,1433;"
             "Database=your_db_name;"
             "Uid=your_username;"
             "Pwd=your_password;"
             "Encrypt=yes;"
             "TrustServerCertificate=no;"
             "Connection Timeout=30;"
         )
         connection_string = f"mssql+pyodbc:///?odbc_connect={params}"
         engine = create_engine(connection_string)
         
      
    2. Increase Connection Timeout: The default connection timeout might be too short for your application, especially if it’s performing complex operations or dealing with large data. Try increasing the Connection Timeout parameter in your connection string.
    3. Check Network Security: Since you mentioned using VNet integration, ensure that the network security rules allow traffic between your web app and the SQL Server. The SQL Server should be accessible from the subnet that’s integrated with your app.
    4. SQL Server Configuration: Verify that the SQL Server is running and configured to accept connections. Check the SQL Server logs for any errors or warnings that might indicate a problem.

    Remember to replace placeholder values like your_server_name, your_db_name, your_username, and your_password with your actual Azure SQL Server details. Also, ensure that sensitive information such as passwords is securely managed and not hardcoded in your application code.

    If you continue to experience issues, it might be helpful to create a minimal, reproducible example and seek further assistance from the community or Azure support. Keep in mind that intermittent issues can be particularly challenging to diagnose, so gathering as much information as possible will be key to finding a solution.


0 additional answers

Sort by: Most helpful