gpt4 book ai didi

mysql - 尝试多次查询时出现 "Unable to acquire a connection"

转载 作者:行者123 更新时间:2023-11-29 15:27:54 30 4
gpt4 key购买 nike

我正在我的node.js 项目中使用MySQL 数据库。我用 Knex 创建了一个数据库查询,结果没问题。但是当我再次尝试查询时,出现此错误:

Error: Unable to acquire a connection
at Client_MySQL.acquireConnection
(C:\Users\Darek\Desktop\proj\node_modules\knex\lib\client.js:336:30)
at Runner.ensureConnection

这是我的knexfile.js:

const dotenv = require('dotenv');
dotenv.config();
module.exports = {
client: 'mysql',
connection: {
host: 'localhost',
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASS,
database: 'testDB'
}
};

然后我必须重新启动我的 npm。我寻找了这个问题的解决方案,但没有适合我的有效答案。

我又看到了这个错误:

(node:8428) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是一个发生错误的代码。它在我的用户模型中的功能:

module.exports.check = (number) => {
var bbb = 0;
return knex
.from('employ')
.select('ID')
.where('emp_number', '=', number)
.then((row) => {

bbb = row.length;
return(bbb);
})
.finally(() => {
knex.destroy();
})



};

并且有一个这个函数的调用:

const numberExist = await User.check(req.body.number);

最佳答案

您不需要调用knex.destroy(),而且在大多数情况下,您可能不应该这样做。如果您有一系列测试或一次性脚本,则 destroy 非常有用,但对于需要针对一个又一个请求持续运行的服务器,您希望 Knex 管理自己的连接池。我建议删除您的 finally block ,并进一步确保您优雅地处理错误(使用 catch):

try {
const numberExist = await User.check(req.body.number);
// ... do something with numberExist ...
} catch (e) {
console.error('Uh-oh:', e.message);
res.status(500).json({ error: "Something unexpected happened!" });
}

另请注意,您的查询是 COUNT,因此这样做会更有效:

module.exports.check = number => 
knex('employ')
.count('ID')
.where('emp_number', number)

关于mysql - 尝试多次查询时出现 "Unable to acquire a connection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58940035/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com