mysql - 如何在 NodeJS 中的相同代码中返回 promise 并关闭 mysql 连接

标签 mysql node.js promise database-connection

我们希望我们的函数返回一个 promise 并关闭数据库连接。我们将 Promise 放在 try 中,并将连接关闭命令放在 finally 中,它会给出以下错误 -

conPool.close()  //It says  Error - conPool.close() is not a function

conPool.release() // Error - conPool.release() is not a function

conPool.releaseConnection(); // Error - Cannot read property '_pool' 
                                          of undefined

请帮我关闭 mySQL 连接。我想返回我的数据。但返回后没有任何效果,因此连接不会关闭。我担心我可能会失去最大限制。我什至在 mySQL 中设置了最大连接数,但仍然如此。有一个可能的解决方案,我可以在函数外部声明 conPool 并使所有函数都使用这个单一的 conPool,但这也不起作用。

代码-

function viewChoice() {


    var sqlQuery;

        sqlQuery = "select * from TRANSACTION_PAYLOAD where INTERFACE_NAME 
     = 'Vehicle RC' AND (STATUS ='INPUT_ERROR' OR STATUS ='ERROR')";



    }


    var deferred = Q.defer();



    var host = config.host;
    var user = config.user;
    var password = config.password;
    var database = config.database;



    var conPool = mysql.createPool({
        host: host,
        user: user,
        password: password,
        database: database
    });


try{
    conPool.getConnection(function (err) {
        console.log("Inside getConnection ")
        if (err)
            deferred.reject(err.name + ': ' + err.message);

        conPool.query(sqlQuery,
            function (err, result, fields) {
                if (err) deferred.reject(err.name + ': ' + err.message);


                console.log(result);
                deferred.resolve(result);
            });
    });
    return deferred.promise;
}

finally{
    console.log("inside  Finally")
    conPool.releaseConnection();

}

}

最佳答案

来自 MySQL 连接器 docs :

Closing all the connections in a pool When you are done using the pool, you have to end all the connections or the Node.js event loop will stay active until the connections are closed by the MySQL server. This is typically done if the pool is used in a script or when trying to gracefully shutdown a server. To end all the connections in the pool, use the end method on the pool:

pool.end(函数(错误){ //池中的所有连接都已结束 });

The end method takes an optional callback that you can use to know when all the connections are ended.

Once pool.end is called, pool.getConnection and other operations can no longer be performed. Wait until all connections in the pool are released before calling pool.end. If you use the shortcut method pool.query, in place of pool.getConnection → connection.query → connection.release, wait until it completes.

pool.end calls connection.end on every active connection in the pool. This queues a QUIT packet on the connection and sets a flag to prevent pool.getConnection from creating new connections. All commands / queries already in progress will complete, but new commands won't execute.

另外,您可以将 return deferred.promise; 移动到 finally block 内

关于mysql - 如何在 NodeJS 中的相同代码中返回 promise 并关闭 mysql 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53867666/

相关文章:

mysql - 如何将sequelize查询的结果推送到数组

javascript - 如何使用 jQuery $.getScript() 方法包含多个 js 文件

mysql - 如何在postgres中将两个select语句设为两列

php - 将 Codeigniter 购物车数据插入数据库

php - 动态数据库项目的设计理念

mysql - 使用 select、group by 和 count 时如何获得非空结果集?

node.js - AWS 无服务器函数 : Can't access methods of aws-sdk objects. 。 ."is not a function"

javascript - Sequelize 五5,通天七7 : TypeError: Class constructor Model cannot be invoked without 'new'

javascript - 通过 ajax 调用在 componentDidMount 中正确设置测试状态

javascript - 在 javascript 中使用 promise 和原型(prototype)时优雅的回调绑定(bind)