javascript 连接超时时Nodejs mssql未处理承诺

7cwmlq89  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(158)

情况很简单:我正在使用npm mssql包来建立数据库连接使用池的方法。初始化池我只是做:

const poolPromise = new sql.ConnectionPool(config)
  .connect()
  .then(pool => {
    console.log('<---- POOL CONNECTED TO MSSQL ---->')
    return pool;
  }, error => {
    console.log('<---- DATABASE IS DOWN ---->')
  }).catch(err => {
    // HERE I LOG THE ERROR SOMEWHERE ELSE
    console.log('Database Connection Failed! Bad Config: ', err)
  });

module.exports = {
  sql, poolPromise
}

现在,问题是数据库服务器现在有点不稳定。有时它会宕机一段时间。这是DB人员必须解决的问题,但我希望能够通知UI DB在某个时候宕机。然而,引发的异常并没有被catch块捕获。它只是在nodejs控制台中写入以下内容:

(node:2962) UnhandledPromiseRejectionWarning: ConnectionError: Failed to connect to <serverIP>:1433 - connect ETIMEDOUT <severIP>:1433
    at Connection.tedious.once.err (/project/node_modules/mssql/lib/tedious.js:239:17)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at Connection.EventEmitter.emit (domain.js:441:20)
    at Connection.socketError (/project/node_modules/tedious/lib/connection.js:1024:14)
    at /project/node_modules/tedious/lib/connection.js:868:25
    at Socket.onError (/project/node_modules/tedious/lib/connector.js:49:9)
    at Socket.emit (events.js:189:13)
    at Socket.EventEmitter.emit (domain.js:441:20)
    at emitErrorNT (internal/streams/destroy.js:82:8)
(node:2962) 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: 2)
(node:2962) [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.

----

你们有什么想法如何捕捉这个特定的事件被正确记录。

xu3bshqb

xu3bshqb1#

示例化池时,可以侦听“error”事件,并在回调中相应地处理它。
常量池=新的Sql.池()
池.on(“错误”,()=〉{
//相应地处理错误}

相关问题