diff --git a/lib/istoredp.js b/lib/istoredp.js index 4f610c7c..d002f8ed 100644 --- a/lib/istoredp.js +++ b/lib/istoredp.js @@ -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; }); }); }); @@ -96,4 +113,4 @@ const db2Call = (callback,xlib,xdatabase,xuser,xpassword,xipc,xctl,xml_input_scr } } -exports.db2Call = db2Call; \ No newline at end of file +exports.db2Call = db2Call;