knexjs承诺释放池连接

我目前使用knexjs.org ,承诺而不是常规callback,并使用池连接进行SQL查询。 第一次运行顺利。 但是现在我通常面临连接池的错误。 代码是这样的

knex('user_detail') .select('id','full_name','phone','email') .where('id', id_user) .then((result) => { resolve(result); }) .catch((error) => { reject(error); }) 

但是现在我通常会得到错误连接超时和错误池连接。 为什么它得到一个错误的第一件事可能是因为我没有释放连接,但我有这样的代码,

 knex('user_detail') .select('id','full_name','phone','email') .where('id', id_user) .then((result) => { resolve(result); }) .catch((error) => { reject(error); }) .finally(() => { knex.destroy() }) 

它适用于第一次尝试,但第二次尝试失败并出现错误There is no pool defined on the current client ,有时错误The pool is probably full

有人可以向我解释发生了什么事情,我怎么解决它? 谢谢。

没有足够的信息可以说明你为什么耗尽游泳池连接。

你调用一些resolve()reject()函数的方式给人一种直觉,认为你使用的是低效或完全错误的promise …

如果你添加完整的代码示例,你如何能够得到该the pool is probably full错误,我可以编辑答案,并能够帮助更多。 例如,通过意外创build多个未解决的事务,该池将被填满。

在第二个代码示例中,您调用knex.destroy() ,该方法不会破坏单个池连接,但会完全破坏您正在使用的knex实例和池。

因此,在knex.destroy()您将无法再使用该knex实例,并且必须通过重新提供数据库连接configuration来创build全新的实例。