【问题标题】:Mysql Connection vs Connection PoolMysql 连接与连接池
【发布时间】:2020-10-05 03:23:41
【问题描述】:

我在同一个数据库中有 4 个单独的表。使用 mysql2.createConnection() 或 mysql2.createPool 批量插入每个表会更好吗?我想异步运行插入。

代码将执行来自 AWS Lambda 的插入,并通过 RDS 代理完成连接,RDS 代理处理与 Aurora MySql 数据库实例的所有连接的连接池。

const mysql2 = require('mysql2');

const connection = mysql2.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

mysql2.createPool

const mysql2 = require('mysql2');

const pool  = mysql2.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret' 
});

【问题讨论】:

    标签: node.js aws-lambda proxy mysql2 connection-pool


    【解决方案1】:

    如果您想异步运行插入,您将需要 createPool。

    因为在 createConnection 中,只有 1 个连接,并且在该连接上执行的所有查询都排队,这并不是真正的异步。 (从 node.js 的角度来看是异步的,但查询是按顺序执行的)

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2012-11-08
      • 2011-10-07
      相关资源
      最近更新 更多