Skip to content

Commit

Permalink
fix: Properly close stmt and conn when using idb (#232)
Browse files Browse the repository at this point in the history
Deleting the statement object doesn't free the underlying resources,
causing memory leaks. Instead, we need to close both the connection and
the statement to free these resources. We can let the runtime delete the
objects as well.

Fixes #230
  • Loading branch information
kadler authored Apr 9, 2020
1 parent 98b8b3e commit b638de8
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions lib/istoredp.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,50 @@ const db2Call = (callback,xlib,xdatabase,xuser,xpassword,xipc,xctl,xml_input_scr
[xml_input_script, db.SQL_PARAM_INPUT, 0],
[xmlOut, db.SQL_PARAM_OUTPUT, 0],
];


const cleanup = () => {
stmt.close();
conn.disconn();
conn.close();
};

if(sync == true) { // Sync Mode
stmt.prepareSync(sql);
stmt.bindParamSync(bindParams);
stmt.executeSync((outArray) => { //out is an array of the output parameters.
if(outArray.length == 1)
callback(outArray[0]); // For XML service, there is always only one return XML output. So handle it directly.
else
callback(outArray); // For multiple return result, caller should handle it as an array.
delete stmt;
conn.disconn();
delete conn;
});
try {
stmt.prepareSync(sql);
stmt.bindParamSync(bindParams);
stmt.executeSync((outArray) => { //out is an array of the output parameters.

if(outArray.length == 1)
callback(outArray[0]); // For XML service, there is always only one return XML output. So handle it directly.
else
callback(outArray); // For multiple return result, caller should handle it as an array.
});
}
finally {
// Ensure we clean up
cleanup();
}
} else { // Async Mode
stmt.prepare(sql, (err) => {
if(err) throw err;
if(err) {
cleanup();
throw err;
}

stmt.bindParam(bindParams, (err) => {
if(err) throw err;
if(err) {
cleanup();
throw err;
}

stmt.execute((outArray, err) => { //out is an array of the output parameters.
cleanup();
if(err) throw err;

if(outArray.length == 1)
callback(outArray[0]); // For XML service, there is always only one return XML output. So handle it directly.
else
callback(outArray); // For multiple return result, caller should handle it as an array.
delete stmt;
conn.disconn();
delete conn;
});
});
});
Expand All @@ -96,4 +113,4 @@ const db2Call = (callback,xlib,xdatabase,xuser,xpassword,xipc,xctl,xml_input_scr
}
}

exports.db2Call = db2Call;
exports.db2Call = db2Call;

0 comments on commit b638de8

Please sign in to comment.