【发布时间】: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