4

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 not timeout without attempting to execute a command thus without reading/writing through the socket?

Massimo
  • 1,653
  • 12
  • 11

1 Answers1

1

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:
        ...