最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to close sql connection when using mysql2promise? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share asked Feb 1, 2017 at 18:24 lem0nifylem0nify 7931 gold badge6 silver badges16 bronze badges 1
  • I don't think release is the correct function name. The reference documentation implies it's end or destroy. – tadman Commented Feb 1, 2017 at 18:26
Add a ment  | 

1 Answer 1

Reset to default 10

Since mysql2 mostly keeps API patibility with mysql, you should be able to close the connection with connection.end().

发布评论

评论列表(0)

  1. 暂无评论