【问题标题】:MySQL connection closed using poolConnectionMySQL 连接使用 poolConnection 关闭
【发布时间】:2019-11-15 20:51:07
【问题描述】:

我的 node.js 文件通过 MySQL 模块与 MySQL 数据库交互。现在我基本上只是创建一个池

var connection = mysql.createPool(dataConnection);

然后我使用connection.query() 进行简单查询。

有时我会收到一条错误消息Error: Connection lost: The server closed the connection. 据我了解,每当我调用查询方法时,都会从池中创建一个全新的连接,然后在完成后立即关闭它。因此我很困惑:既然服务器应该为这个查询显式创建一个连接,怎么能关闭连接?而且,最重要的是,有什么方法可以让我真正避免这个问题(幸运的是,这种情况并不经常发生,但仍然如此)。

感谢您的帮助!

诺埃尔。

【问题讨论】:

    标签: mysql node.js


    【解决方案1】:

    由于您没有分享很多与连接池创建相关的细节和代码,我会尽力回答。

        var pool = mysql.createPool(dataConnection);
        // check if you have configured any timeouts etc for the pool
    

    我不确定确切的代码,但应该如下所示:

    connection = pool.GetConnection();
    result = connection.query();
    // now instead of closing the connection, it should be returned/released back to the pool
    connection.release();
    

    How do I create a MySQL connection pool while working with NodeJS and Express?

    【讨论】:

      猜你喜欢
      • 2014-08-17
      • 2014-01-10
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 2014-12-18
      • 2012-11-03
      • 1970-01-01
      相关资源
      最近更新 更多