How to close sql connection when using mysql2/promise?

lem0nify

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?

mscdex

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to close or reset a pconnect() connection when using PHPRedis and PHP-FPM?

When to close MySQL connection using node-mysql?

when to close connection in mongodb?

Do I need to close connection when using a 'using' statement

In Apache Spark SQL, How to close metastore connection from HiveContext

How to close HTTP connection in node.js using Express

Do i need to close the connection when using parametrised connection

How server knows to close the connection of websocket when website tab is closed

flask - When to close mysql connection?

How to close an HTTP connection?

OleDbConnection: How to open and close a connection using a function

How do I close serial connection when I interrupt code

How to close a local server connection using Swift in the terminal?

How to auto close a MySQL connection in python when an error occurred during query execution?

How to handle ECONNRESET when proxied response includes `connection: close` header?

How to close a SolrClient connection?

How to end DB connection using Mysql2 Promise?

how to close the connection when exiting the shiny app

Close connection in sql - Timeout

SQL Connection Remains Open when using DataContext

How to close JNDI connection?

How to close a datasource mysql-connection

Sql Connection refuses to connect when using Mono

How to close database connection?

How to keep Firebase Connection on iOS when you close and reopen the app?

How to close MySQL connection after multiple promise?

How can I force close excon connection when using chunked request

Error when do Promise.all (close connection / hang up)

When to close MySQL connection discord.py