Skip to content

fix: Properly close stmt and conn when using idb #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;