2

I actually use this MYSQL Client and when I close a connection is actually never closes , so I can see in the status.

router.get('/test', function (req, res, next) {

    var conn = mysql.createConnection(config);

    conn.connect();

    conn.query('select  * from invoices ', function (err, result) {

        if (err) {
            throw err;
        }

        res.status(200).json({result: result});
        conn.end();// || conn.destroy();
    });

});

Connections

2 Answers 2

4

Move conn.end() out of the query callback - as described in node-mysql's documentation:

Every method you invoke on a connection is queued and executed in sequence.

Closing the connection is done using end() which makes sure all remaining queries are executed before sending a quit packet to the mysql server.

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I try that and it didn't work , also I try with the method conn.destroy() and didn't work.
Having the same issue, end() and destroy() do not work.
0

Also you can use pool.

Check this link.

Connections can be pooled to ease sharing a single connection, or managing multiple connections.

When you are done with a connection, just call connection.release() and the connection will return to the pool, ready to be used again by someone else.

pool.end(function (err) {
  // all connections in the pool have ended
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.