Node-mysql connection end or destroy does not work

I do not know why mysql.end () or mysql.destroy () do not work as I expect. This is my code.

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    database: 'db',
    password: ''
});
connection.connect();
connection.query('SELECT * FROM ofertas ', function (err, rows, fields)
{
    if (err) console.log(err);
    else
    {
        console.log(rows);
    }

});
connection.destroy(function (err)
{
    if (err) throw err;
});

I execute this code and then I go to the mysql command line and I type this:

show status like 'Conn%';

To node code

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections   | 3     |
+---------------+-------+

After node code

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections   | 4     |
+---------------+-------+

connection.state = 'disconnected'

thank

+4
source share
1 answer

You need to wait for the request to complete before you can destroy the connection. Try

connection.query('SELECT * FROM ofertas ', function (err, rows, fields)
{
    if (err) console.log(err);
    else
    {
        console.log(rows);
    }
    connection.destroy();
});
0
source

All Articles