Is this the correct way to release the connection?

As you know, Node.js statements are asynchronous. Therefore, I write the code as follows. But I cannot be sure if the connection is released or not.

Why, because I write connection.release()

right after res.json()

. In my opinion, after sending the response, the control does not proceed to the next statement (Query 1 : is this true?). So should I write it before posting a response?

Request 2 : res.json() / res.end()

and connection.release()

both are asynchronous. It's true?

Request 3 . Even if the following code works fine, can I connection.release()

only write it once, i.e. when does the operator end if else

? If so why next()

doesn't it work after res.end () `in Node.js?

connection.query(query, [body.endDate, tripId], function (err, rows, fields) {
    if (err) {
        res.status(422).json(
            {
                status: "error",
                data: {error: {code: err.code, sqlMessage: err.sqlMessage}},
                message: "Something didn't work."
            }
        );
        connection.release();
    } else {
        res.status(200).json(
            {
                status: "success",
                data: {affectedRows: rows.affectedRows},
                message: "Trip ended."
            }
        );
        connection.release();
    }
});

      

+3


source to share





All Articles