Skip to content

Commit b638de8

Browse files
authored
fix: Properly close stmt and conn when using idb (#232)
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
1 parent 98b8b3e commit b638de8

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

lib/istoredp.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,50 @@ const db2Call = (callback,xlib,xdatabase,xuser,xpassword,xipc,xctl,xml_input_scr
6060
[xml_input_script, db.SQL_PARAM_INPUT, 0],
6161
[xmlOut, db.SQL_PARAM_OUTPUT, 0],
6262
];
63-
63+
64+
const cleanup = () => {
65+
stmt.close();
66+
conn.disconn();
67+
conn.close();
68+
};
69+
6470
if(sync == true) { // Sync Mode
65-
stmt.prepareSync(sql);
66-
stmt.bindParamSync(bindParams);
67-
stmt.executeSync((outArray) => { //out is an array of the output parameters.
68-
if(outArray.length == 1)
69-
callback(outArray[0]); // For XML service, there is always only one return XML output. So handle it directly.
70-
else
71-
callback(outArray); // For multiple return result, caller should handle it as an array.
72-
delete stmt;
73-
conn.disconn();
74-
delete conn;
75-
});
71+
try {
72+
stmt.prepareSync(sql);
73+
stmt.bindParamSync(bindParams);
74+
stmt.executeSync((outArray) => { //out is an array of the output parameters.
75+
76+
if(outArray.length == 1)
77+
callback(outArray[0]); // For XML service, there is always only one return XML output. So handle it directly.
78+
else
79+
callback(outArray); // For multiple return result, caller should handle it as an array.
80+
});
81+
}
82+
finally {
83+
// Ensure we clean up
84+
cleanup();
85+
}
7686
} else { // Async Mode
7787
stmt.prepare(sql, (err) => {
78-
if(err) throw err;
88+
if(err) {
89+
cleanup();
90+
throw err;
91+
}
92+
7993
stmt.bindParam(bindParams, (err) => {
80-
if(err) throw err;
94+
if(err) {
95+
cleanup();
96+
throw err;
97+
}
98+
8199
stmt.execute((outArray, err) => { //out is an array of the output parameters.
100+
cleanup();
82101
if(err) throw err;
102+
83103
if(outArray.length == 1)
84104
callback(outArray[0]); // For XML service, there is always only one return XML output. So handle it directly.
85105
else
86106
callback(outArray); // For multiple return result, caller should handle it as an array.
87-
delete stmt;
88-
conn.disconn();
89-
delete conn;
90107
});
91108
});
92109
});
@@ -96,4 +113,4 @@ const db2Call = (callback,xlib,xdatabase,xuser,xpassword,xipc,xctl,xml_input_scr
96113
}
97114
}
98115

99-
exports.db2Call = db2Call;
116+
exports.db2Call = db2Call;

0 commit comments

Comments
 (0)