Search code examples
node.jsmysql2

Synch implementation mysql queries in node


Use mysql2.

Is it true, that if we set connectionLimit to 1, then mysql queries will be executed in series (not parallel)?

For example:

const mysql = require('mysql2');

const pool = mysql.createPool({
  host: 'localhost',
  user: 'name',
  password:'password',
  database: 'database',
  waitForConnections: true,
  connectionLimit: 1,
  queueLimit: 0
}).promise();


pool.query("SELECT 'long' as query_result FROM table where big_column like '%sometext%'").then(r=>{ console.log(r[0])}); // long query
pool.query("SELECT 'fast' as query_result").then(r=>{console.log(r[0])}); // fast query

// Result:
// [ TextRow { query_result: 'long'} ]
// [ TextRow { query_result: 'fast' } ]

Solution

  • Yes, the next queries will be queued until the connection of previous query was released back to pool.

    Additional, if you set waitForConnections to be false, the next queries will return error instead move to queued