【问题标题】:MySQL returns query, but the connection does not closeMySQL返回查询,但连接没有关闭
【发布时间】:2021-01-23 08:47:48
【问题描述】:

我正在尝试运行此 js 代码来访问 MySQL/MariaDB 服务器(此时,我都尝试了)。

const knex = require('../connection')
const mysql = require('mysql2');
 

function getDifficulty(){
    /*
    return knex.select('*').from('tb_dificuldades').limit(9)
        .then((results) => {
            console.log("GOT RESULTS!")
            return results
        })
    */
    // create the connection to database
    const connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '',
        database: ''
    })
    const results = connection.query(
        'SELECT * FROM tb_dificuldades;',
        function(err, results, fields) {
          console.log(results); // results contains rows returned by server
          console.log(fields); // fields contains extra meta data about results, if available
          return results
        }
    )
    return results
 
}

getDifficulty()

require('dotenv').config()
const path = require('path')

module.exports = {
    development: {
        client: 'mysql2',
        version: '5.7',
        connection: {
            host: process.env.DB_HOST || 'localhost',
            user: process.env.DB_USER || 'root',
            password: process.env.DB_PASSWORD || '',
            database: process.env.DB_NAME || '',
        },
        migrations: {
            tableName: 'knex_migrations',
            directory: `${path.resolve(__dirname, 'src', 'database', 'migrations')}`
        },
        seeds: {
            directory: `${path.resolve(__dirname, 'src', 'database', 'seeds')}`
        }
    }
}

require('dotenv').config()

const knexfile = require('../../knexfile')
const knex = require('knex')(knexfile[process.env.ENV || 'development'])

module.exports = knex

打印结果和字段,但连接没有关闭,因此代码停止在回调函数上执行。

环境:

OS:Debian GNU/Linux 10 (buster)
D:Docker version 19.03.13
DC:docker-compose version 1.21.0
DB:MariaDB & MySQL (docker)
IP:localhost(docker-proxy); db(docker-dns)
CLIENT: mysql & mysql2 (nodejs); knexjs

我已经尝试过从 mysql 更改为 mariadb;
本来我是用带mysql的knex,然后用mysql2,然后用mysql2不带knex;
我正在通过 localhost (docker-proxy) 使用 DBeaver,并且相同的查询运行良好;
Knex 迁移和种子也在起作用;

预期:

[...]
 columnType: 3,
    flags: 4097,
    decimals: 0
  }
]

[Done] exited with code=0 in 0.305 seconds

得到:

[...]
 columnType: 3,
    flags: 4097,
    decimals: 0
  }
]

[Done] exited with code=null in 18.594 seconds

OBS: exited after aborting process, also this query shouldn't take 18s
and it isn't taking that long.
It does return what I expect, but the client holds the connection.
So, my program freezes after running the query. (Not happening in DBeaver)

EDIT1:使用 knex.destroy() 程序可以正常工作,但这是预期的用法吗?

return knex.select('*').from('tb_dificuldades').limit(9)
        .then((results) => {
            console.log("GOT RESULTS!")
            console.log(results)
            knex.destroy()
            return results
        })

【问题讨论】:

  • 您能解释一下您的问题和您的期望吗?
  • 你好@Hendry,刚刚添加了更多信息

标签: mysql node.js mariadb knex.js knexjs


【解决方案1】:

每次查询后您不需要关闭连接,只需在应用程序的其他部分重用它即可。如果您确定需要关闭连接,只需手动关闭即可。

例如使用 Knex,您可以这样做

knex.destroy();

example

documentation

【讨论】:

  • 当然,我不需要破坏连接。但是程序卡住了,mysql返回查询结果后
  • 尝试在您的查询回调中销毁连接,看看是否有帮助
  • 是的,破坏连接有帮助。该程序工作得很好。但我以前从未以这种方式使用过 knex。这是mysql的预期行为吗?大多数情况下,我将它与 postgres 一起使用。见编辑 1
  • 所有连接都应该在应用程序结束时关闭。通常框架会在后台执行此操作,但有时您必须手动处理它。所以作为一个答案 - 是的,从我的角度来看,这是预期的行为
  • 是的,我知道。我在将mysql与python一起使用时已经这样做了。但是例如,在 mysql2 文档中,它们仅在使用池时关闭连接。和我用 knex 做的其他代码,我没有明确地关闭连接。如果是js版本的问题,我想知道?我之前用 ts 用过。
猜你喜欢
  • 1970-01-01
  • 2021-09-26
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 2014-01-04
  • 2018-08-20
  • 1970-01-01
  • 2021-05-10
相关资源
最近更新 更多