最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin2PV0评论

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().

Post a comment

comment list (0)

  1. No comments so far