sequelize创建的连接数多于连接池中允许的最大连接数

时间:2018-06-06 07:15:50

标签: mysql node.js sequelize.js

在我的连接池设置中,我允许最多10个连接。但是在show processlist中我可以看到alomost 13个线程与不同的Id连接。以下是我的连接池设置。

setState

sequelize.js

pool: {
    max: 10,
    min: 0,
    acquire: 30000,
    idle: 10000,
  },

models.js

const Sequelize = require('sequelize');
const createModels = require('./models');

async function init(dbName) {
  try {
    const MySQLSettings = getAWSConfig('MySQL');
    const { username, password, port, host } = MySQLSettings;
    const sequelize = new Sequelize(dbName, username, password, {
      host,
      port,
      dialect: 'mysql',

      pool: {
        max: 10,
        min: 0,
        acquire: 30000,
        idle: 10000,
      },
      define: {
        timestamps: false,
      },
    });

    await sequelize.authenticate();
    Logger.info('Connection established');

    const models = createModels(sequelize);
    return {
      sequelize,
      ...models,
    };
  } catch (error) {
    Logger.error(error);
    throw error;
  }
}


module.exports = init;

以下是我已将sequelize对象传递给所有服务的服务文件。

services.js

const Sequelize = require('sequelize');
const Logger = require('../logger');

function createModels(sequelize) {
  try {
    const Module1 = sequelize.define('Module1', {
     ...
     ...
    }, {
      timestamps: false,
      version: true,
    });

    ...
    ...

    return {
      Module1,
      Module2,
      ...
   };
  } catch (error) {
    Logger.error(error);
    throw error;
  }
}

module.exports = createModels;

我是否为每项服务创建了新的连接池?

0 个答案:

没有答案