Be consistent and always raise cx_Oracle.InterfaceError when a connection is

closed and unusable.
This commit is contained in:
Anthony Tuininga 2019-11-29 14:58:47 -07:00
parent 428746f4de
commit 5f70edd71c
2 changed files with 32 additions and 3 deletions

View File

@ -596,6 +596,8 @@ static PyObject *cxoConnection_changePassword(cxoConnection *conn,
int status; int status;
// parse the arguments // parse the arguments
if (cxoConnection_isConnected(conn) < 0)
return NULL;
if (!PyArg_ParseTuple(args, "OO", &oldPasswordObj, &newPasswordObj)) if (!PyArg_ParseTuple(args, "OO", &oldPasswordObj, &newPasswordObj))
return NULL; return NULL;
@ -1027,6 +1029,8 @@ static int cxoConnection_setCallTimeout(cxoConnection* conn, PyObject *value,
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static PyObject *cxoConnection_getType(cxoConnection *conn, PyObject *nameObj) static PyObject *cxoConnection_getType(cxoConnection *conn, PyObject *nameObj)
{ {
if (cxoConnection_isConnected(conn) < 0)
return NULL;
return (PyObject*) cxoObjectType_newByName(conn, nameObj); return (PyObject*) cxoObjectType_newByName(conn, nameObj);
} }
@ -1082,6 +1086,8 @@ static PyObject *cxoConnection_getVersion(cxoConnection *conn, void *unused)
char buffer[25]; char buffer[25];
int status; int status;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
status = dpiConn_getServerVersion(conn->handle, NULL, NULL, &versionInfo); status = dpiConn_getServerVersion(conn->handle, NULL, NULL, &versionInfo);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
@ -1181,10 +1187,13 @@ static PyObject *cxoConnection_close(cxoConnection *conn, PyObject *args)
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
status = dpiConn_close(conn->handle, mode, (char*) tagBuffer.ptr, status = dpiConn_close(conn->handle, mode, (char*) tagBuffer.ptr,
tagBuffer.size); tagBuffer.size);
if (status == DPI_SUCCESS)
dpiConn_release(conn->handle);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
cxoBuffer_clear(&tagBuffer); cxoBuffer_clear(&tagBuffer);
if (status < 0) if (status < 0)
return cxoError_raiseAndReturnNull(); return cxoError_raiseAndReturnNull();
conn->handle = NULL;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -1300,6 +1309,8 @@ static PyObject *cxoConnection_newCursor(cxoConnection *conn, PyObject *args,
PyObject *createArgs, *result, *arg; PyObject *createArgs, *result, *arg;
Py_ssize_t numArgs = 0, i; Py_ssize_t numArgs = 0, i;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
if (args) if (args)
numArgs = PyTuple_GET_SIZE(args); numArgs = PyTuple_GET_SIZE(args);
createArgs = PyTuple_New(1 + numArgs); createArgs = PyTuple_New(1 + numArgs);
@ -1342,6 +1353,8 @@ static PyObject *cxoConnection_cancel(cxoConnection *conn, PyObject *args)
static PyObject *cxoConnection_newEnqueueOptions(cxoConnection *conn, static PyObject *cxoConnection_newEnqueueOptions(cxoConnection *conn,
PyObject *args) PyObject *args)
{ {
if (cxoConnection_isConnected(conn) < 0)
return NULL;
return (PyObject*) cxoEnqOptions_new(conn, NULL); return (PyObject*) cxoEnqOptions_new(conn, NULL);
} }
@ -1353,6 +1366,8 @@ static PyObject *cxoConnection_newEnqueueOptions(cxoConnection *conn,
static PyObject *cxoConnection_newDequeueOptions(cxoConnection *conn, static PyObject *cxoConnection_newDequeueOptions(cxoConnection *conn,
PyObject *args) PyObject *args)
{ {
if (cxoConnection_isConnected(conn) < 0)
return NULL;
return (PyObject*) cxoDeqOptions_new(conn, NULL); return (PyObject*) cxoDeqOptions_new(conn, NULL);
} }
@ -1379,6 +1394,8 @@ static PyObject *cxoConnection_newMessageProperties(cxoConnection *conn,
&payloadObj, &correlationObj, &delay, &exceptionQObj, &expiration, &payloadObj, &correlationObj, &delay, &exceptionQObj, &expiration,
&priority)) &priority))
return NULL; return NULL;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
// create new message properties object // create new message properties object
props = cxoMsgProps_new(conn, NULL); props = cxoMsgProps_new(conn, NULL);
@ -1481,6 +1498,8 @@ static PyObject *cxoConnection_dequeue(cxoConnection *conn, PyObject* args,
&nameObj, &cxoPyTypeDeqOptions, &optionsObj, &cxoPyTypeMsgProps, &nameObj, &cxoPyTypeDeqOptions, &optionsObj, &cxoPyTypeMsgProps,
&propertiesObj, &cxoPyTypeObject, &payloadObj)) &propertiesObj, &cxoPyTypeObject, &payloadObj))
return NULL; return NULL;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
if (cxoBuffer_fromObject(&nameBuffer, nameObj, if (cxoBuffer_fromObject(&nameBuffer, nameObj,
conn->encodingInfo.encoding) < 0) conn->encodingInfo.encoding) < 0)
return NULL; return NULL;
@ -1526,6 +1545,8 @@ static PyObject *cxoConnection_enqueue(cxoConnection *conn, PyObject* args,
&nameObj, &cxoPyTypeEnqOptions, &optionsObj, &cxoPyTypeMsgProps, &nameObj, &cxoPyTypeEnqOptions, &optionsObj, &cxoPyTypeMsgProps,
&propertiesObj, &cxoPyTypeObject, &payloadObj)) &propertiesObj, &cxoPyTypeObject, &payloadObj))
return NULL; return NULL;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
if (cxoBuffer_fromObject(&nameBuffer, nameObj, if (cxoBuffer_fromObject(&nameBuffer, nameObj,
conn->encodingInfo.encoding) < 0) conn->encodingInfo.encoding) < 0)
return NULL; return NULL;
@ -1566,6 +1587,8 @@ static PyObject *cxoConnection_queue(cxoConnection *conn, PyObject* args,
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|O!", keywordList, if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|O!", keywordList,
&nameObj, &cxoPyTypeObjectType, &typeObj)) &nameObj, &cxoPyTypeObjectType, &typeObj))
return NULL; return NULL;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
if (cxoBuffer_fromObject(&nameBuffer, nameObj, if (cxoBuffer_fromObject(&nameBuffer, nameObj,
conn->encodingInfo.encoding) < 0) conn->encodingInfo.encoding) < 0)
return NULL; return NULL;
@ -1596,6 +1619,8 @@ static PyObject *cxoConnection_queue(cxoConnection *conn, PyObject* args,
static PyObject *cxoConnection_contextManagerEnter(cxoConnection *conn, static PyObject *cxoConnection_contextManagerEnter(cxoConnection *conn,
PyObject* args) PyObject* args)
{ {
if (cxoConnection_isConnected(conn) < 0)
return NULL;
Py_INCREF(conn); Py_INCREF(conn);
return (PyObject*) conn; return (PyObject*) conn;
} }
@ -1743,6 +1768,8 @@ static PyObject *cxoConnection_subscribe(cxoConnection *conn, PyObject* args,
&params.groupingValue, &params.groupingType, &name, &params.groupingValue, &params.groupingType, &name,
&clientInitiatedObj)) &clientInitiatedObj))
return NULL; return NULL;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
if (cxoUtils_getBooleanValue(clientInitiatedObj, 0, if (cxoUtils_getBooleanValue(clientInitiatedObj, 0,
&params.clientInitiated) < 0) &params.clientInitiated) < 0)
return NULL; return NULL;
@ -1832,6 +1859,8 @@ static PyObject *cxoConnection_unsubscribe(cxoConnection *conn, PyObject* args,
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O!", keywordList, if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O!", keywordList,
&cxoPyTypeSubscr, &subscrObj)) &cxoPyTypeSubscr, &subscrObj))
return NULL; return NULL;
if (cxoConnection_isConnected(conn) < 0)
return NULL;
// destroy ODPI-C subscription // destroy ODPI-C subscription
subscr = (cxoSubscr*) subscrObj; subscr = (cxoSubscr*) subscrObj;

View File

@ -201,7 +201,7 @@ class TestCase(TestEnv.BaseTestCase):
"confirm an exception is raised after closing a connection" "confirm an exception is raised after closing a connection"
connection = TestEnv.GetConnection() connection = TestEnv.GetConnection()
connection.close() connection.close()
self.assertRaises(cx_Oracle.DatabaseError, connection.rollback) self.assertRaises(cx_Oracle.InterfaceError, connection.rollback)
def testConnectWithHandle(self): def testConnectWithHandle(self):
"test creating a connection using a handle" "test creating a connection using a handle"
@ -301,7 +301,7 @@ class TestCase(TestEnv.BaseTestCase):
cursor.execute("insert into TestTempTable (IntCol) values (1)") cursor.execute("insert into TestTempTable (IntCol) values (1)")
connection.commit() connection.commit()
cursor.execute("insert into TestTempTable (IntCol) values (2)") cursor.execute("insert into TestTempTable (IntCol) values (2)")
self.assertRaises(cx_Oracle.DatabaseError, connection.ping) self.assertRaises(cx_Oracle.InterfaceError, connection.ping)
connection = TestEnv.GetConnection() connection = TestEnv.GetConnection()
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute("select count(*) from TestTempTable") cursor.execute("select count(*) from TestTempTable")
@ -342,7 +342,7 @@ class TestCase(TestEnv.BaseTestCase):
if TestEnv.GetClientVersion() >= (12, 1): if TestEnv.GetClientVersion() >= (12, 1):
attrNames.append("ltxid") attrNames.append("ltxid")
for name in attrNames: for name in attrNames:
self.assertRaises(cx_Oracle.DatabaseError, getattr, connection, self.assertRaises(cx_Oracle.InterfaceError, getattr, connection,
name) name)
def testPing(self): def testPing(self):