Azure Database Support Blog
1 MIN READ
Lesson Learned #355: Testing the connection latency from Python to Azure SQL Database
Jose_Manuel_Jurado
May 24, 2023Microsoft
In some situations, we need to test the connectivity latency using Python. Here you could find a small script about how to do it.
import pyodbc
import time
def ConnectToTheDB():
try:
print('Connecting to the DB')
start_time = time.time()
conn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};server=tcp:servername.database.windows.net,1433;UID=username;PWD=password;database=DBName;APP=Testing Connection;timeout=30;MARS_Connection=no");
print("Connected to the Database %s seconds ---" % ((time.time() - start_time)) )
return conn
except BaseException as e:
print("An error occurred connecting to the DB - " + format(e))
return
SQL = "select 1"
nLoop=1
while nLoop<1000:
nLoop=nLoop+1
conn = ConnectToTheDB()
cursor = conn.cursor()
start_time = time.time()
cursor.execute(SQL)
row = cursor.fetchone()
print("---------------- Loop:%d - %s seconds ---" % (nLoop,(time.time() - start_time)) )
conn.close()
Please, review several considerations about ODBC connection:
- Lesson Learned #243: Enabling ODBC connection pooling in PHP - Microsoft Community Hub
- Lesson Learned #38: Which is the impact using connection pooling in my application - Microsoft Community Hub
- Lesson Learned #188: Tracing/logging the JDBC and ODBC driver operation - Microsoft Community Hub
- Lesson Learned #193: MARS vs Connection Retry-Logic - Microsoft Community Hub
- Lesson Learned #236: What? .... How much time you said? (CloudShell version) - Microsoft Community Hub
Enjoy!
Published May 24, 2023
Version 1.0Jose_Manuel_Jurado
Microsoft
Joined November 29, 2018
Azure Database Support Blog
Follow this blog board to get notified when there's new activity