mysql - 在不破坏连接的情况下使用池会使系统挂起

标签 mysql node.js aws-lambda

如果我将此代码部署到 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() - 这没有用,就像第一个例子一样挂起。

最佳答案

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

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

关于mysql - 在不破坏连接的情况下使用池会使系统挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211080/

相关文章:

java - 客户看不到新记录

node.js/ express : Sending static files if they exist

node.js - 使用 Node 创建带有内置对象的 Google 文档

c# - 使用 AmazonServerlessApplicationRepositoryClient 部署 AWS 无服务器 lambda 应用程序不起作用?

mysql - mysql是如何反向解析IP地址的?

MySQL并发从行中选择

php - 检查数据库连接,否则显示消息

node.js - 将 shapefile 和 geoJson 转换为 TopoJson 和/或使用 geo2topo

file - 如何使用 go in lambda 安全地检查 S3 存储桶中是否存在文件?

python - 没有名为“cryptography.hazmat.bindings._padding”的模块