从Sequelize.dialect.connectionManager.getConnection()方法获取的乏味Connection是否会被自动销毁?

时间:2018-10-09 02:18:02

标签: node.js sequelize.js

我正在使用Sequelize在mssql中使用输出参数调用存储过程,但是看来sequelize不支持out读取输出参数值,但是乏味地支持它:

function test() {
    return new Promise((resolve, reject) => {
        var connection = new Connection(tediousConfig);
        connection.on("connect", err => {
            if (err)
                reject(err);

            console.log("connected");
            let returnedValues = [];
            var request = new Request("ReturnOneValue", (err, rowCount, rows) => {
                if (err)
                    reject(err);

                let returnedValue = new Object();
                for (let pair of returnedValues)
                    returnedValue[pair.key] = pair.value;
                resolve(returnedValue);
            });
            request.on("returnValue", (key, value, metadata) => {
                returnedValues.push({ key, value });
            });
            request.addOutputParameter("Value", TYPES.Int);
            connection.callProcedure(request);
        });
    });
}

我想在Sequelize中使用该池,因此在从github中读取了一些代码之后,我找到了一种获取基础乏味连接的方法:

function sqlServerSequelizeConnection() {
  return sqlServerSequelize.dialect.connectionManager.getConnection({
    priority: 1, //mid
    type: "read",
    useMaster: false
  })
  .then(resourceLock => Promise.resolve(resourceLock.resource));
}

有效:

function tediousConnectionTest() {
    return sqlServerSequelizeConnection()
        .then(connection => {
            connection.on('end', () => {
                console.log("connection closed"); //never trigger
            });
            return new Promise((resolve, reject) => {
                let returnedValues = [];
                var request = new Request("ReturnOneValue", (err, rowCount, rows) => {
                    if (err)
                        reject(err);

                    let returnedValue = new Object();
                    for (let pair of returnedValues)
                        returnedValue[pair.key] = pair.value;
                    resolve(returnedValue);
                });
                request.on("returnValue", (key, value, metadata) => {
                    returnedValues.push({ key, value });
                });
                request.addOutputParameter("Value", TYPES.Int);
                connection.callProcedure(request);
            });
        });
}

但是在sequelizeConfig.pool.idle时间之后,没有触发繁琐的连接的“结束”事件,我应该手动关闭连接吗?

0 个答案:

没有答案