如何在Node中正确关闭与MySQL的连接?

huangapple go评论58阅读模式
英文:

How to properly close connections to mysql in node?

问题

这里是你要翻译的代码部分:

// myDb.mjs
export const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'test'
});

// myFunctions.mjs
export function query() {
  db.query('SELECT * FROM table', (error, res) => {
    console.log(res)
  })
}

请注意,以上内容是代码,无需进行翻译。

英文:

So, for example, we have a function that performs some kind of transaction, it gets imported in different parts of the application where it keeps being called.

// myDb.mjs
export const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'test'
});


// myFunctions.mjs
export function query() {
  db.query('SELECT * FROM table', (error, res) => {
    console.log(res)
  })
}

Now, in PHP using PDO the connection lives until the scripts ends (see related: Is it necessary to close PDO connections), How do I handle this in node using mysql ?

  1. I see that there are connection.end(), connection.destroy(), When to call them, after the query executes, at the end of the script, do I have to end the connection manually ?
  2. Does the server opens a connection to the database every time a fetch reqeust comes through from the client ?

答案1

得分: 2

Ideally you don't close them, you return them to a connection pool. Database connections are often a contentious resource so you should create them only when necessary.

In the code you have here, a singular connection is created in the module and used for all subsequent operations. If that connection fails, expires, or is otherwise incapacitated, your program will just emit errors.

I'd recommend using a light abstraction layer like Sequelize. This has connection pool support, issuing connections that are tested before issuing, and as a bonus your code becomes more database agnostic.

You'll also want to use promises, as callback-driven code gets extraordinarily messy really fast.

英文:

Ideally you don't close them, you return them to a connection pool. Database connections are often a contentious resource so you should create them only when necessary.

In the code you have here, a singular connection is created in the module and used for all subsequent operations. If that connection fails, expires, or is otherwise incapacitated, your program will just emit errors.

I'd recommend using a light abstraction layer like Sequelize. This has connection pool support, issuing connections that are tested before issuing, and as a bonus your code becomes more database agnostic.

You'll also want to use promises, as callback-driven code gets extraordinarily messy really fast.

答案2

得分: 0

A better approach is to use a managed pool of connections. It cashes a set of connections and reuses them when necessary. This method enhances the performance and the response time as well. The pool takes care of opening, reusing, and closing the individual connections.

var pool = await mysql.createPool(connectionStr);
...
...

//When the application closes
pool.close()

英文:

A better approach is to use a managed pool of connections. It cashes a set of connections and reuses them when necessary. This method enhances the performance and the response time as well. The pool takes care of opening, reusing, and closing the individual connections.

 var pool = await mysql.createPool(connectionStr);
 ...
 ...

 //When the application closes
 pool.close()

huangapple
  • 本文由 发表于 2023年5月25日 01:08:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定