Understanding Differences in Query Behavior in mysql Connection Pools

Asked 1 years ago, Updated 1 years ago, 48 views

const mysql=require("mysql");
const config = {
    host: '127.0.0.1',
    user: 'root',
    password:',
    database: "test",
    connectionLimit:10
}
const pool = mysql.createPool(config);
letstrQuery='SELECT* FROM `test';
// g Run query from getConnection
pool.getConnection(function(err,conn){
    if(err)throwerr;
    conn.query(strQuery, function(error, results, fields){
        if(error)throwerror;
        console.log(results);
        conn.release();
    });
});
// 直接 Run query directly
pool.query(strQuery, function(error, results, fields){
    if(error)throwerror;
    console.log(results);
});

How is the behavior of these two queries different internally?

javascript mysql node.js

2022-09-30 20:13

1 Answers

https://github.com/mysqljs/mysql#closing-all-the-connections-in-a-pool

...the pool.query method is a short-hand for the pool.getConnection->connection.query->connection.release()...

pool.query() is a short hand that handles a series of pool.getConnection()->connection.query()->pool.release() flows, so it retrieves, queries, and closes connection pools internally.If you want to issue multiple queries to a single connection, it is recommended that you do not use pool.query() but hold the connection in pool.getConnection() before handling multiple queries within the callback.


2022-09-30 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.