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 ?
- 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 ? - Does the server opens a connection to the database every time a fetch reqeust comes through from the client ?