The Python Oracle

What is the return value of Connection.ping() in cx_oracle?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4

--

Chapters
00:00 Question
01:06 Accepted answer (Score 6)
02:54 Answer 2 (Score 8)
03:15 Thank you

--

Full question
https://stackoverflow.com/questions/4266...

Question links:
[cx_oracle]: https://pypi.python.org/pypi/cx_Oracle/
[the cx_oracle document]: http://cx-oracle.readthedocs.io/en/lates...

Accepted answer links:
[cx_Oracle documentation states that]: http://cx-oracle.readthedocs.io/en/lates...
[connection methods]: https://www.python.org/dev/peps/pep-0249...
[optional extensions]: https://www.python.org/dev/peps/pep-0249...
[has this method]: https://dev.mysql.com/doc/connector-pyth...
[the code for ]: https://bitbucket.org/anthony_tuininga/c...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #oracle #cxoracle

#avk47



ANSWER 1

Score 9


Thanks to Ben's answer, given a connectionObject from the cx_Oracle library, this should at least incapsulate what you might want.

def isOpen(connectionObject):
    try:
        return connectionObject.ping() is None
    except:
        return False



ACCEPTED ANSWER

Score 6


The cx_Oracle documentation states that this is:

This method is an extension to the DB API definition and is only available in Oracle 10g R2 and higher.

However, this method isn't documented in PEP 249 - the current Python Database API specification in either the connection methods or optional extensions (or anywhere else for that matter).

The MySQL implementation of PEP 249 also has this method; the documentation states that:

When the connection is not available, an InterfaceError is raised. Use the is_connected() method to check the connection without raising an error.

Raises InterfaceError on errors.

As this is identical to the cx_Oracle behaviour I would assume that this is the answer to your question and what you've determined the behaviour to be is correct:

  • There is no return if the connection is active
  • An InterfaceError is raised if the connection is not active

If we look at the code for .ping(), it confirms that this is the manner in which the method has been implemented:

static PyObject *Connection_Ping(
    udt_Connection *self,               // connection
    PyObject* args)                     // arguments
{
    sword status;

    if (Connection_IsConnected(self) < 0)
        return NULL;
    status = OCIPing(self->handle, self->environment->errorHandle,
            OCI_DEFAULT);
    if (Environment_CheckForError(self->environment, status,
            "Connection_Ping()") < 0)
        return NULL;
    Py_INCREF(Py_None);
    return Py_None;
}