Skip to content

Commit 5f70edd

Browse files
Be consistent and always raise cx_Oracle.InterfaceError when a connection is
closed and unusable.
1 parent 428746f commit 5f70edd

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/cxoConnection.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ static PyObject *cxoConnection_changePassword(cxoConnection *conn,
596596
int status;
597597

598598
// parse the arguments
599+
if (cxoConnection_isConnected(conn) < 0)
600+
return NULL;
599601
if (!PyArg_ParseTuple(args, "OO", &oldPasswordObj, &newPasswordObj))
600602
return NULL;
601603

@@ -1027,6 +1029,8 @@ static int cxoConnection_setCallTimeout(cxoConnection* conn, PyObject *value,
10271029
//-----------------------------------------------------------------------------
10281030
static PyObject *cxoConnection_getType(cxoConnection *conn, PyObject *nameObj)
10291031
{
1032+
if (cxoConnection_isConnected(conn) < 0)
1033+
return NULL;
10301034
return (PyObject*) cxoObjectType_newByName(conn, nameObj);
10311035
}
10321036

@@ -1082,6 +1086,8 @@ static PyObject *cxoConnection_getVersion(cxoConnection *conn, void *unused)
10821086
char buffer[25];
10831087
int status;
10841088

1089+
if (cxoConnection_isConnected(conn) < 0)
1090+
return NULL;
10851091
Py_BEGIN_ALLOW_THREADS
10861092
status = dpiConn_getServerVersion(conn->handle, NULL, NULL, &versionInfo);
10871093
Py_END_ALLOW_THREADS
@@ -1181,10 +1187,13 @@ static PyObject *cxoConnection_close(cxoConnection *conn, PyObject *args)
11811187
Py_BEGIN_ALLOW_THREADS
11821188
status = dpiConn_close(conn->handle, mode, (char*) tagBuffer.ptr,
11831189
tagBuffer.size);
1190+
if (status == DPI_SUCCESS)
1191+
dpiConn_release(conn->handle);
11841192
Py_END_ALLOW_THREADS
11851193
cxoBuffer_clear(&tagBuffer);
11861194
if (status < 0)
11871195
return cxoError_raiseAndReturnNull();
1196+
conn->handle = NULL;
11881197

11891198
Py_RETURN_NONE;
11901199
}
@@ -1300,6 +1309,8 @@ static PyObject *cxoConnection_newCursor(cxoConnection *conn, PyObject *args,
13001309
PyObject *createArgs, *result, *arg;
13011310
Py_ssize_t numArgs = 0, i;
13021311

1312+
if (cxoConnection_isConnected(conn) < 0)
1313+
return NULL;
13031314
if (args)
13041315
numArgs = PyTuple_GET_SIZE(args);
13051316
createArgs = PyTuple_New(1 + numArgs);
@@ -1342,6 +1353,8 @@ static PyObject *cxoConnection_cancel(cxoConnection *conn, PyObject *args)
13421353
static PyObject *cxoConnection_newEnqueueOptions(cxoConnection *conn,
13431354
PyObject *args)
13441355
{
1356+
if (cxoConnection_isConnected(conn) < 0)
1357+
return NULL;
13451358
return (PyObject*) cxoEnqOptions_new(conn, NULL);
13461359
}
13471360

@@ -1353,6 +1366,8 @@ static PyObject *cxoConnection_newEnqueueOptions(cxoConnection *conn,
13531366
static PyObject *cxoConnection_newDequeueOptions(cxoConnection *conn,
13541367
PyObject *args)
13551368
{
1369+
if (cxoConnection_isConnected(conn) < 0)
1370+
return NULL;
13561371
return (PyObject*) cxoDeqOptions_new(conn, NULL);
13571372
}
13581373

@@ -1379,6 +1394,8 @@ static PyObject *cxoConnection_newMessageProperties(cxoConnection *conn,
13791394
&payloadObj, &correlationObj, &delay, &exceptionQObj, &expiration,
13801395
&priority))
13811396
return NULL;
1397+
if (cxoConnection_isConnected(conn) < 0)
1398+
return NULL;
13821399

13831400
// create new message properties object
13841401
props = cxoMsgProps_new(conn, NULL);
@@ -1481,6 +1498,8 @@ static PyObject *cxoConnection_dequeue(cxoConnection *conn, PyObject* args,
14811498
&nameObj, &cxoPyTypeDeqOptions, &optionsObj, &cxoPyTypeMsgProps,
14821499
&propertiesObj, &cxoPyTypeObject, &payloadObj))
14831500
return NULL;
1501+
if (cxoConnection_isConnected(conn) < 0)
1502+
return NULL;
14841503
if (cxoBuffer_fromObject(&nameBuffer, nameObj,
14851504
conn->encodingInfo.encoding) < 0)
14861505
return NULL;
@@ -1526,6 +1545,8 @@ static PyObject *cxoConnection_enqueue(cxoConnection *conn, PyObject* args,
15261545
&nameObj, &cxoPyTypeEnqOptions, &optionsObj, &cxoPyTypeMsgProps,
15271546
&propertiesObj, &cxoPyTypeObject, &payloadObj))
15281547
return NULL;
1548+
if (cxoConnection_isConnected(conn) < 0)
1549+
return NULL;
15291550
if (cxoBuffer_fromObject(&nameBuffer, nameObj,
15301551
conn->encodingInfo.encoding) < 0)
15311552
return NULL;
@@ -1566,6 +1587,8 @@ static PyObject *cxoConnection_queue(cxoConnection *conn, PyObject* args,
15661587
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|O!", keywordList,
15671588
&nameObj, &cxoPyTypeObjectType, &typeObj))
15681589
return NULL;
1590+
if (cxoConnection_isConnected(conn) < 0)
1591+
return NULL;
15691592
if (cxoBuffer_fromObject(&nameBuffer, nameObj,
15701593
conn->encodingInfo.encoding) < 0)
15711594
return NULL;
@@ -1596,6 +1619,8 @@ static PyObject *cxoConnection_queue(cxoConnection *conn, PyObject* args,
15961619
static PyObject *cxoConnection_contextManagerEnter(cxoConnection *conn,
15971620
PyObject* args)
15981621
{
1622+
if (cxoConnection_isConnected(conn) < 0)
1623+
return NULL;
15991624
Py_INCREF(conn);
16001625
return (PyObject*) conn;
16011626
}
@@ -1743,6 +1768,8 @@ static PyObject *cxoConnection_subscribe(cxoConnection *conn, PyObject* args,
17431768
&params.groupingValue, &params.groupingType, &name,
17441769
&clientInitiatedObj))
17451770
return NULL;
1771+
if (cxoConnection_isConnected(conn) < 0)
1772+
return NULL;
17461773
if (cxoUtils_getBooleanValue(clientInitiatedObj, 0,
17471774
&params.clientInitiated) < 0)
17481775
return NULL;
@@ -1832,6 +1859,8 @@ static PyObject *cxoConnection_unsubscribe(cxoConnection *conn, PyObject* args,
18321859
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O!", keywordList,
18331860
&cxoPyTypeSubscr, &subscrObj))
18341861
return NULL;
1862+
if (cxoConnection_isConnected(conn) < 0)
1863+
return NULL;
18351864

18361865
// destroy ODPI-C subscription
18371866
subscr = (cxoSubscr*) subscrObj;

test/Connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def testExceptionOnClose(self):
201201
"confirm an exception is raised after closing a connection"
202202
connection = TestEnv.GetConnection()
203203
connection.close()
204-
self.assertRaises(cx_Oracle.DatabaseError, connection.rollback)
204+
self.assertRaises(cx_Oracle.InterfaceError, connection.rollback)
205205

206206
def testConnectWithHandle(self):
207207
"test creating a connection using a handle"
@@ -301,7 +301,7 @@ def testCtxMgrClose(self):
301301
cursor.execute("insert into TestTempTable (IntCol) values (1)")
302302
connection.commit()
303303
cursor.execute("insert into TestTempTable (IntCol) values (2)")
304-
self.assertRaises(cx_Oracle.DatabaseError, connection.ping)
304+
self.assertRaises(cx_Oracle.InterfaceError, connection.ping)
305305
connection = TestEnv.GetConnection()
306306
cursor = connection.cursor()
307307
cursor.execute("select count(*) from TestTempTable")
@@ -342,7 +342,7 @@ def testClosedConnectionAttributes(self):
342342
if TestEnv.GetClientVersion() >= (12, 1):
343343
attrNames.append("ltxid")
344344
for name in attrNames:
345-
self.assertRaises(cx_Oracle.DatabaseError, getattr, connection,
345+
self.assertRaises(cx_Oracle.InterfaceError, getattr, connection,
346346
name)
347347

348348
def testPing(self):

0 commit comments

Comments
 (0)