How to check if database connection is expired in python
Hello Community, I am connecting python with Vertica but after some time connection is getting expired, I really want that when the connection gets expired my status gets changed In mysql we have connection.is_connected() method which is true if the connection is still maintained but if it expires it gives false , Do we have a similar function in Vertica
Thanks
@I-akshay Yes, vertica-python has similar functions: connection.opened() and connection.closed() return Boolean values.
Can you tell me what's the current behavior when your connection is getting expired?
HI @sitingren Thanks for your response, really appreciate, but it is not working
For example :
conn_info = {'host': '10.63.4.301', 'port': 5433, 'database': 'personaldwh','user': 'username', 'password': 'username123'} connection = vertica_python.connect(**conn_info) cur = connection.cursor()
Now lets says i wait for 45 mins now,
cur.execute("Select 1") This statement is throwing isssue (Connection Closed by vertica) but when I am doing if connection.opened(): print("Yes")
It is still showing Yes
Can you tell me what's the current behavior when your connection is getting expired? Basically i am calling a loop from where i am running a shell command and wait for 100 mins by the time my connection is getting closed by vertica
I really appreciate if you tell me how to handle this problem
This connection.opened() function generally cannot be called to determine whether a connection to a database is valid or invalid, it does passive checking. Unfortunately, there is no function to actively determine if a connection is expired.
You can determine that a connection is expired by catching exceptions that might be thrown when an operation is attempted. select 1 is a good dummy statement to execute. So you are doing the right thing.