sequelize连接管理器无法正确关闭连接

时间:2018-12-19 13:26:42

标签: node.js sequelize.js

我的代码如下所示:

db.js

const Sequelize = require('sequelize');
require('dotenv').config();
const fs = require('fs');
const path = require('path');


const db = {};


var sequelize = new Sequelize({
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    username: process.env.DB_USER_NAME,
    password: process.env.DB_PASSWORD,
    dialect: process.env.DB_TYPE,
    port: process.env.DB_PORT,
    pool: {
        max: 5,
        min: 0,
        idle: 100000,
        acquire: 50000,
        evict: 50000,
        handleDisconnects: true
    },
    operatorsAliases: false,
    logging: false
});

sequelize
    .authenticate()
    .then(() => {
        console.log('connected to database successfully')
    });


sequelize.sync({
    force: false
});

db.Sequelize = Sequelize;
db.sequelize = sequelize;

db.admin = require('../model/admin')(sequelize, Sequelize);


module.exports = db;

activity.js

var  db = require('./../utils/db.js');

const registerAdmin = (req, res) => {

    req.checkBody('email_id', 'Please enter valid email id').notEmpty().isEmail();
    req.checkBody('password', 'Please enter the password').notEmpty();


    req.sanitize('email_id').trim();
    req.sanitize('password').trim();


    const errors = req.validationErrors();

    if (errors) {
        console.log('.................error', errors);
        let error = '';
        errors.forEach(element => {
            error = error + ' ' + element.msg;
        });
        response.sendErrorCustomMessage(res, error, "500");
    } else {

        checkUser(req.body.email_id).then(() => {
            createUser(req.body.email_id, req.body.password, req.body.admin_role).then((info) => {
                db.sequelize.connectionManager.close();
                response.sendsuccessData(res, 'Admin Created', info)
            }).catch((err) => {
                response.sendErrorMessage(res, consts.ERROR_500);
            })

        }).catch((err) => {
            response.sendErrorCustomMessage(res, err, consts.ERROR_500);
        })
    }
};

但是以某种方式却给了我如下所示的错误:

0|app  | Error: ConnectionManager.getConnection was called after the connection manager was closed!
0|app  |     at ConnectionManager.getConnection (/home/ubuntu/s-backend/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:86:29)
0|app  |     at Promise.try (/home/ubuntu/s-backend/node_modules/sequelize/lib/sequelize.js:554:34)
0|app  |     at tryCatcher (/home/ubuntu/s-backend/node_modules/bluebird/js/release/util.js:16:23)
0|app  |     at Function.Promise.attempt.Promise.try (/home/ubuntu/s-backend/node_modules/bluebird/js/release/method.js:39:29)
0|app  |     at retryParameters (/home/ubuntu/s-backend/node_modules/sequelize/lib/sequelize.js:454:48)
0|app  |     at /home/ubuntu/selltm-backend/node_modules/retry-as-promised/index.js:39:21
0|app  |     at Promise._execute (/home/ubuntu/s-backend/node_modules/bluebird/js/release/debuggability.js:303:9)
0|app  |     at Promise._resolveFromExecutor (/home/ubuntu/s-backend/node_modules/bluebird/js/release/promise.js:483:18)
0|app  |     at new Promise (/home/ubuntu/s-backend/node_modules/bluebird/js/release/promise.js:79:10)
0|app  |     at retryAsPromised (/home/ubuntu/s-backend/node_modules/retry-as-promised/index.js:29:10)
0|app  |     at Sequelize.query (/home/ubuntu/s-backend/node_modules/sequelize/lib/sequelize.js:454:12)
0|app  |     at QueryInterface.select (/home/ubuntu/s-backend/node_modules/sequelize/lib/query-interface.js:1104:27)
0|app  |     at Promise.try.then.then.then (/home/ubuntu/s-backend/node_modules/sequelize/lib/model.js:1604:34)
0|app  |     at tryCatcher (/home/ubuntu/s-backend/node_modules/bluebird/js/release/util.js:16:23)
0|app  |     at Promise._settlePromiseFromHandler (/home/ubuntu/s-backend/node_modules/bluebird/js/release/promise.js:512:31)
0|app  |     at Promise._settlePromise (/home/ubuntu/s-backend/node_modules/bluebird/js/release/promise.js:569:18)
0|app  |     at Promise._settlePromise0 (/home/ubuntu/s-backend/node_modules/bluebird/js/release/promise.js:614:10)
0|app  |     at Promise._settlePromises (/home/ubuntu/s-backend/node_modules/bluebird/js/release/promise.js:693:18)
0|app  |     at Async._drainQueue (/home/ubuntu/s-backend/node_modules/bluebird/js/release/async.js:133:16)
0|app  |     at Async._drainQueues (/home/ubuntu/s-backend/node_modules/bluebird/js/release/async.js:143:10)
0|app  |     at Immediate.Async.drainQueues (/home/ubuntu/s-backend/node_modules/bluebird/js/release/async.js:17:14)
0|app  |     at Immediate.args.(anonymous function) (/usr/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:138:29)
0|app  |     at runCallback (timers.js:794:20)
0|app  |     at tryOnImmediate (timers.js:752:5)
0|app  |     at processImmediate [as _immediateCallback] (timers.js:729:5)

使用mysql show status like 'Conn%';

它为我提供了以下指标:

+-----------------------------------+--------+
| Variable_name                     | Value  |
+-----------------------------------+--------+
| Connection_errors_accept          | 0      |
| Connection_errors_internal        | 0      |
| Connection_errors_max_connections | 597    |
| Connection_errors_peer_address    | 0      |
| Connection_errors_select          | 0      |
| Connection_errors_tcpwrap         | 0      |
| Connections                       | 386139 |
+-----------------------------------+--------+

0 个答案:

没有答案