I'm facing an issue where removing the client.close() seems to solve the problem, but I can't leave connections open to the database after the function is done. It doesn't feel secure to me. Even though the initial write completes successfully, when I attempt a second write, I encounter an error. I have my connect function in the try block and closing the connection in the finally block, but it seems like the connection might be permanently closed after the first write or maybe the close operation is happening before the write. I'm looking for some guidance on how to handle this situation.
async function Connecting() {
try {
await client.connect();
await createListing(client, req.body, req.query.dbname);
await res.send("written to " + req.query.dbname);
} catch (e) {
console.error(e);
res.send("unable to write due to " + `${e}`);
} finally {
await client.close();
console.log("connection closed");
}
}