简体   繁体   中英

How to close sql connection when using mysql2/promise?

I have such nodejs code:

var mysql = require('mysql2/promise');

mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "123123",
    database: "mydatabase"
})
.then((connection) => connection.execute("SELECT * FROM mytable"))
.then(([rows, fields]) => {
    console.log(rows);
});

When I execute it, application prints array of database rows and is still running after that. Seems it happens because connection isn't released. How to release it after console.log ?

I've tried this way:

var mysql = require('mysql2/promise');

mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "123123",
    database: "mydatabase"
})
.then((connection) => {
    connection.execute("SELECT * FROM mytable")
    .then(([rows, fields]) => {
        console.log(rows);
        connection.release();
    });
});

but result is TypeError: this.connection.release is not a function .

Sorry for my English.

Any ideas?

由于mysql2主要保持与mysql API兼容性,因此您应该能够使用connection.end()关闭连接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM