【问题标题】:Using pool without destroying connection makes system to hang在不破坏连接的情况下使用池会使系统挂起
【发布时间】:2017-07-20 09:44:08
【问题描述】:

如果我将此代码部署到 Amazon Lambda ...

var settings = require('./settings');
var mysql = require('mysql');
var pool = mysql.createPool({
    host: settings.host,
    database: settings.database,
    user: settings.user,
    password: settings.password
});

module.exports.handler = function (request, context, callback) {
    pool.getConnection(function (error, connection) {
        if (error)
            return callback(error);
        connection.query('select 1', null, function (error, results) {
            callback(null, results);
        });
    });
};

...端点以超时错误结束:

{
    "errorMessage": "2017-07-20T10:04:26.629Z cc46e503-6d32-11e7-8fa9-9902efa96cc1 Task timed out after 6.00 seconds"
}

如果我添加 connection.destroy - 此代码成功完成:

...
    connection.query('select 1', null, function (error, results) {
        connection.destroy();
        callback(null, results);
    });
...

但我认为破坏连接不是一个好习惯。 如果我使用 connection.release() 而不是 connection.destroy() - 这没有帮助,就像第一个示例一样挂起。

【问题讨论】:

  • 你试过使用connection.release指定npmjs.com/package/mysql#pooling-connections
  • @SukhmeetSingh - 我在原始消息中写道 - 是的,没有帮助。
  • 我认为只有销毁将连接提交回池。 Pages that use connection pooling, on the other hand, maintain open connections in a pool. When the page requires access to the database, it simply uses an existing connection from the pool, and establishes a new connection only if no pooled connections are available.npmjs.com/package/mysql#pooling-connections

标签: mysql node.js aws-lambda


【解决方案1】:

解决方案是将 context.callbackWaitsForEmptyEventLoop = false; 添加到我的处理程序中:

module.exports.handler = function (request, context, callback) {
   context.callbackWaitsForEmptyEventLoop = false;
   ....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-13
    • 2020-06-15
    • 2021-02-25
    • 1970-01-01
    • 2018-11-03
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多