Tools: Fixing Azure SQL Connection Errors In Azure Scheduled Python Job

Tools: Fixing Azure SQL Connection Errors In Azure Scheduled Python Job

Posted on Feb 27

• Originally published at blog.luca-liu.com

As a Data Analyst, I recently faced a frustrating issue while automating a daily data processing task in Azure.

The goal was simple: run a scheduled job every morning to process data and sync it to an Azure SQL Database. When I ran the code manually, it worked perfectly. But when the scheduled job (via Azure Functions or Synapse) triggered at 6:00 AM, it crashed immediately.

Here is the solution to fixing the "Database not available" error without increasing your Azure bill.

I am using the Azure SQL Database Serverless tier. To save costs, this tier features Auto-pause. If no one uses the database for a set period (e.g., 1 hour), Azure puts it to sleep.

When my scheduled job runs in the morning, the database is cold. It takes approximately 60 to 90 seconds for Azure to spin the compute back up. The default Python connection string gives up before the database is ready.

The result: The error stopped, but my costs tripled. I was paying for compute 24/7 for a job that only runs for 10 minutes a day. This is not efficient.

Instead of keeping the server running all night, we should write code that is patient enough to wait for the server to wake up.

I wrote a custom wrapper for the SQLAlchemy engine that handles the specific behavior of Azure Serverless cold starts.

Here is the robust connection function. It attempts to connect, and if it detects the database is sleeping, it waits and retries until the server is back online.

Source: Dev.to