cx_oracle中Connection.ping()的返回值是什么?

问题描述:

我刚刚安装了一个python包:cx_oracle。从the cx_oracle document我发现了一种方法:Connection.ping(),它被描述为“Ping可用于测试连接是否仍处于活动状态的服务器”。cx_oracle中Connection.ping()的返回值是什么?

但该文件没有提到ping()的返回值是什么。

我写了一些代码,做一个测试:

#!/usr/bin/env python3 
import cx_Oracle 
conn = cx_Oracle.connect("...") 
print(conn.ping()) # display:None 
conn.close() 
print(conn.ping()) # raise exception: cx_Oracle.InterfaceError: not connected 

从测试结果来看,我发现ping()将返回None是否连接正常或抛出一个异常:连接关闭后cx_Oracle.InterfaceError: not connected

是否还有其他可能的返回值?为什么不只是返回True或False?

cx_Oracle documentation states that这是:

此方法的一个扩展DB API定义,并且仅在Oracle 10g中R2可用和更高。

然而,这种方法不记录在PEP 249 - 当前Python数据库API规范无论是在connection methodsoptional extensions(或其他为此事的任何地方)。

PEP 249的MySQL实现也是has this method;该文档指出:

当连接不可用时,将引发一个InterfaceError。使用 的is_connected()方法检查连接而不会引起 错误。

因错误引发InterfaceError

由于这是等同于cx_Oracle行为我会假设,这是回答你的问题,你已经确定什么样的行为是正确的是:

  • 时如果不归路连接是活动
  • 如果如果我们看一下the code for .ping()的连接处于不活动

一个InterfaceError升高时,就已经证实这是在方式该方法已实施:

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; 
}