Skip to content

Commit a7e2afd

Browse files
committed
During close/terminate/release methods for objects, remove all references in order to reduce memory consumption as much as possible; ensure that failure to close/terminate/release is handled in the same way by all objects (even though for some this is an extremely unlikely situation).
1 parent dbafad3 commit a7e2afd

File tree

9 files changed

+68
-27
lines changed

9 files changed

+68
-27
lines changed

src/njs/src/njsCommon.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,6 @@ njsBaton::~njsBaton()
212212
}
213213

214214

215-
//-----------------------------------------------------------------------------
216-
// njsBaton::GetCallingObj()
217-
// Return the C++ object stored in the baton as the JS calling object.
218-
//-----------------------------------------------------------------------------
219-
njsCommon *njsBaton::GetCallingObj()
220-
{
221-
Nan::HandleScope scope;
222-
Local<Object> obj = Nan::New(jsCallingObj);
223-
return Nan::ObjectWrap::Unwrap<njsCommon>(obj);
224-
}
225-
226-
227215
//-----------------------------------------------------------------------------
228216
// njsBaton::GetOracledb()
229217
// Return the Oracledb object stored in the baton as a JS object.
@@ -293,9 +281,8 @@ void njsBaton::AsyncAfterWorkCallback(uv_work_t *req, int status)
293281
Local<Function> callback = Nan::New<Function>(baton->jsCallback);
294282

295283
// if this baton is considered the active baton, clear it
296-
njsCommon *callingObj = baton->GetCallingObj();
297-
if (callingObj && baton == callingObj->activeBaton)
298-
callingObj->activeBaton = NULL;
284+
if (baton->callingObj && baton == baton->callingObj->activeBaton)
285+
baton->callingObj->activeBaton = NULL;
299286

300287
// delete the baton before the callback is made so any unnecessary
301288
// ODPI-C handles are released as soon as possible

src/njs/src/njsCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class njsBaton {
271271
char *bufferPtr;
272272
uint64_t lobOffset;
273273
uint64_t lobAmount;
274+
njsCommon *callingObj;
274275
Nan::Persistent<Object> jsCallingObj;
275276
Nan::Persistent<Object> jsOracledb;
276277
Nan::Persistent<Object> jsBuffer;

src/njs/src/njsConnection.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,9 @@ void njsConnection::Async_AfterExecute(njsBaton *baton, Local<Value> argv[])
14741474
// njsConnection::Release()
14751475
// Releases the connection from use by JS. This releases the connection back
14761476
// to the pool or closes the standalone connection so further use is not
1477-
// possible.
1477+
// possible. The reference to the DPI handle is transferred to the baton so
1478+
// that it will be cleared automatically upon success and so that the
1479+
// connection is marked as invalid immediately.
14781480
//
14791481
// PARAMETERS
14801482
// - JS callback which will receive (error)
@@ -1498,13 +1500,30 @@ NAN_METHOD(njsConnection::Release)
14981500

14991501
//-----------------------------------------------------------------------------
15001502
// njsConnection::Async_Release()
1501-
// Worker function for njsConnection::Release() method.
1503+
// Worker function for njsConnection::Release() method. If the attempt to
1504+
// close fails, the reference to the DPI handle is transferred back from the
1505+
// baton to the connection.
15021506
//-----------------------------------------------------------------------------
15031507
void njsConnection::Async_Release(njsBaton *baton)
15041508
{
15051509
if (dpiConn_close(baton->dpiConnHandle, DPI_MODE_CONN_CLOSE_DEFAULT, NULL,
1506-
0) < 0)
1510+
0) < 0) {
15071511
baton->GetDPIError();
1512+
njsConnection *connection = (njsConnection*) baton->callingObj;
1513+
connection->dpiConnHandle = baton->dpiConnHandle;
1514+
baton->dpiConnHandle = NULL;
1515+
}
1516+
}
1517+
1518+
1519+
//-----------------------------------------------------------------------------
1520+
// njsConnection::Async_AfterRelease()
1521+
// Finishes release by cleaning up references.
1522+
//-----------------------------------------------------------------------------
1523+
void njsConnection::Async_AfterRelease(njsBaton *baton, Local<Value> argv[])
1524+
{
1525+
njsConnection *connection = (njsConnection*) baton->callingObj;
1526+
connection->jsOracledb.Reset();
15081527
}
15091528

15101529

src/njs/src/njsConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class njsConnection: public njsCommon {
9999
// Release Method on Connection class
100100
static NAN_METHOD(Release);
101101
static void Async_Release(njsBaton *baton);
102+
static void Async_AfterRelease(njsBaton *baton, Local<Value> argv[]);
102103

103104
// Commit Method on Connection class
104105
static NAN_METHOD(Commit);

src/njs/src/njsIntLob.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void njsILob::Async_Read(njsBaton *baton)
346346
void njsILob::Async_AfterRead(njsBaton *baton, Local<Value> argv[])
347347
{
348348
Nan::EscapableHandleScope scope;
349-
njsILob *lob = (njsILob*) baton->GetCallingObj();
349+
njsILob *lob = (njsILob*) baton->callingObj;
350350

351351
if (!baton->bufferSize)
352352
argv[1] = scope.Escape(Nan::Null());
@@ -420,7 +420,7 @@ void njsILob::Async_Write(njsBaton *baton)
420420
//-----------------------------------------------------------------------------
421421
void njsILob::Async_AfterWrite(njsBaton *baton, Local<Value> argv[])
422422
{
423-
njsILob *lob = (njsILob*) baton->GetCallingObj();
423+
njsILob *lob = (njsILob*) baton->callingObj;
424424
lob->offset += baton->lobAmount;
425425
}
426426

src/njs/src/njsPool.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ void njsPool::Async_AfterGetConnection(njsBaton *baton, Local<Value> argv[])
371371

372372
//-----------------------------------------------------------------------------
373373
// njsPool::Terminate()
374-
// Terminate the pool.
374+
// Terminate the pool. The reference to the DPI handle is transferred to the
375+
// baton so that it will cleared automatically upon success and so that the
376+
// pool is marked as invalid immediately.
375377
//
376378
// PARAMETERS
377379
// - JS callback which will receive (error)

src/njs/src/njsPool.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class njsPool: public njsCommon {
8484
// Terminate Methods
8585
static NAN_METHOD(Terminate);
8686
static void Async_Terminate(njsBaton *baton);
87-
static void Async_AfterTerminate(njsBaton *baton, Local<Value> argv[]);
8887

8988
// Define Getter Accessors to properties
9089
static NAN_GETTER(GetPoolMax);

src/njs/src/njsResultSet.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void njsResultSet::Async_AfterGetRows(njsBaton *baton, Local<Value> argv[])
377377
newBaton->maxRows = baton->maxRows - baton->rowsFetched;
378378
newBaton->fetchMultipleRows = true;
379379
newBaton->jsRows.Reset(rows);
380-
resultSet = (njsResultSet*) baton->GetCallingObj();
380+
resultSet = (njsResultSet*) baton->callingObj;
381381
resultSet->activeBaton = NULL;
382382
resultSet->GetRowsCommon(newBaton);
383383

@@ -392,7 +392,9 @@ void njsResultSet::Async_AfterGetRows(njsBaton *baton, Local<Value> argv[])
392392

393393
//-----------------------------------------------------------------------------
394394
// njsResultSet::Close()
395-
// Close the result set.
395+
// Close the result set. The reference to the DPI handle is transferred to
396+
// the baton so that it will be cleared automatically upon success and so that
397+
// the result set is marked as invalid immediately.
396398
//
397399
// PARAMETERS
398400
// - JS callback which will receive (error)
@@ -414,17 +416,46 @@ NAN_METHOD(njsResultSet::Close)
414416
baton->dpiStmtHandle = resultSet->dpiStmtHandle;
415417
resultSet->dpiStmtHandle = NULL;
416418
}
417-
baton->QueueWork("Close", Async_Close, NULL, 1);
419+
baton->QueueWork("Close", Async_Close, Async_AfterClose, 1);
418420
}
419421

420422

421423
//-----------------------------------------------------------------------------
422424
// njsResultSet::Async_Close()
423-
// Worker function for njsResultSet::Close() method.
425+
// Worker function for njsResultSet::Close() method. If the attempt to close
426+
// the statement fails, the reference to the DPI handle is transferred back
427+
// from the baton to the result set.
424428
//-----------------------------------------------------------------------------
425429
void njsResultSet::Async_Close(njsBaton *baton)
426430
{
427-
if (dpiStmt_close(baton->dpiStmtHandle, NULL, 0) < 0)
431+
if (dpiStmt_close(baton->dpiStmtHandle, NULL, 0) < 0) {
428432
baton->GetDPIError();
433+
njsResultSet *resultSet = (njsResultSet*) baton->callingObj;
434+
resultSet->dpiStmtHandle = baton->dpiStmtHandle;
435+
baton->dpiStmtHandle = NULL;
436+
}
437+
}
438+
439+
440+
//-----------------------------------------------------------------------------
441+
// njsResultSet::Async_AfterClose()
442+
// Finishes close by transferring variables and fetch as types to the baton
443+
// where they will be freed.
444+
//-----------------------------------------------------------------------------
445+
void njsResultSet::Async_AfterClose(njsBaton *baton, Local<Value> argv[])
446+
{
447+
njsResultSet *resultSet = (njsResultSet*) baton->callingObj;
448+
449+
resultSet->jsConnection.Reset();
450+
resultSet->jsOracledb.Reset();
451+
baton->keepQueryInfo = false;
452+
baton->queryVars = resultSet->queryVars;
453+
baton->numQueryVars = resultSet->numQueryVars;
454+
resultSet->queryVars = NULL;
455+
resultSet->numQueryVars = 0;
456+
baton->numFetchAsStringTypes = resultSet->numFetchAsStringTypes;
457+
baton->fetchAsStringTypes = resultSet->fetchAsStringTypes;
458+
resultSet->fetchAsStringTypes = NULL;
459+
resultSet->numFetchAsStringTypes = 0;
429460
}
430461

src/njs/src/njsResultSet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class njsResultSet: public njsCommon {
9393
// Close Methods
9494
static NAN_METHOD(Close);
9595
static void Async_Close(njsBaton *baton);
96+
static void Async_AfterClose(njsBaton *baton, Local<Value> argv[]);
9697

9798
// Define Getter Accessors to properties
9899
static NAN_GETTER(GetMetaData);

0 commit comments

Comments
 (0)