Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check if database connection is expired in python #301

Open
I-akshay opened this issue Jul 31, 2019 · 3 comments
Open

How to check if database connection is expired in python #301

I-akshay opened this issue Jul 31, 2019 · 3 comments

Comments

@I-akshay
Copy link

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

@sitingren
Copy link
Member

sitingren commented Jul 31, 2019

@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?

@I-akshay
Copy link
Author

I-akshay commented Aug 1, 2019

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

@sitingren
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants