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?
To close a database connection gracefully, you call the end() method on the connection object. The end() method ensures that all remaining queries are always executed before the database connection closed. To force the connection close immediately, you can use the destroy() method.
If we now: Close our Node server (press CTRL + C at the command line) Start our Node server again by node your-file-name. js.
Since mysql2
mostly keeps API compatibility with mysql
, you should be able to close the connection with connection.end()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With