我正在为 postgres 数据库编写一个 api 服务器,并且正在测试一些压力案例。我正在使用模块 node-postgres 中的一个池,并遇到了这个问题。
在启动服务器之前,我首先用尽了所有 postgers 连接。然后我尝试通过池运行查询。
我尝试在 promise 周围添加 try catch 块。在承诺内(在 catch 块内),没有任何帮助。
以下是我重现错误的一些测试代码。
var pg = require('./node_modules/pg')
var pool = new pg.Pool(
{
user: "test",
password: "pass134",
host: "localhost",
database: "testdb",
port: 5432,
max: 32,
min: 16,
idleTimeoutMillis: 60000,
connectionTimeoutMillis: 10000
}
);
pool.on("error", function(err){
console.log("Error1: " + String(err));
});
pool.query("SELECT COUNT(*) FROM acal_cust", [])
.then(function(err, results){
console.log(err);
console.log(results);
}).catch(function(err){
console.log("Error2:" + String(err));
pool.end();
});
这是我运行代码时得到的。
Error2: error: sorry, too many clients already
(node:10422) UnhandledPromiseRejectionWarning: error: sorry, too many clients already
at Connection.parseE (/ext/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/ext/node_modules/pg/lib/connection.js:381:17)
at Socket.<anonymous> (/ext/node_modules/pg/lib/connection.js:117:22)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:597:20)
(node:10422) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10422) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我想知道如何正确处理错误。这样它就不会被抛出。