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 use Connection.ping() #595

Open
truh opened this issue Dec 7, 2021 · 2 comments
Open

How to use Connection.ping() #595

truh opened this issue Dec 7, 2021 · 2 comments

Comments

@truh
Copy link

truh commented Dec 7, 2021

  1. What is the link to the documentation section that needs improving?

https://cx-oracle.readthedocs.io/en/latest/api_manual/connection.html?highlight=ping#Connection.ping

  1. Describe the confusion

The function doesn't appear to have a return value? How can I use it to check if the connection is still alive? Is an exception raised it the connection is dead?

  1. Suggest changes that would help

Please provide additional information about the return and raise value of the function.

Please provide a usage example.

@cjbj
Copy link
Member

cjbj commented Dec 7, 2021

Yep, we should improve the doc.

Try this:

import cx_Oracle
import os
import platform

if platform.system() == "Darwin":
    cx_Oracle.init_oracle_client(lib_dir=os.environ.get("HOME")+"/Downloads/instantclient_19_8")

username = os.environ.get("PYTHON_USERNAME")
password = os.environ.get("PYTHON_PASSWORD")
connect_string = os.environ.get("PYTHON_CONNECTSTRING")

sql = """select unique 'alter system kill session '''||sid||','||serial#||'''' from v$session_connect_info where sid = sys_context('USERENV', 'SID')"""

connection = cx_Oracle.connect(user=username, password=password, dsn=connect_string)
killsql, = connection.cursor().execute(sql).fetchone()

systemconnection = cx_Oracle.connect(user='system', password='whatever', dsn=connect_string)
systemconnection.cursor().execute(killsql)

try:
    connection.ping()

except cx_Oracle.Error as e:
    error, = e.args
    print(error.message)

This gives me:

DPI-1080: connection was closed by ORA-28

@truh
Copy link
Author

truh commented Dec 7, 2021

Thank you, that has been very helpful.

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