This is a generic question about all python database drivers but if you an answer for a specific driver (pyodbc, psycopg2, pymysql, mysqldb, etc.) would be useful anyway.
Once I have a connection and cursor, is there a way (an API) to check the connection has or has n开发者_Go百科ot timeout without attempting to execute a command thus without reading/writing through the socket?
In psycopg2, there is an attribute in both the cursor and connection objects named "closed".
For example, to check if your cursor still is open:
connection = psycopg2.connect (...)
cursor = connection.cursor()
if cursor.closed:
print('the connection is closed')
else:
...
精彩评论