在 Sequelize 操作之后关闭我的连接是一个好习惯吗? [英] Is it a good practice to close my connection after Sequelize operations?
问题描述
目前我正在使用 NodeJS/TypeScript 在 Sequelize 之上设计一个应用程序,我想知道它是否会导致性能问题而不是关闭连接.
currently I'm designing an app on top of Sequelize using NodeJS/TypeScript, and I'm wondering if it can cause performance issues not closing a connection.
例如,在微服务中,我需要来自 1 个实体的数据.
For instance, in a micro service, I need data from 1 entity.
const resolver = async (,,{db}) => {
const entity1 = await db.models.Entity1.findOne()
return entity1
}
调用findOne
后是否需要关闭连接?
Is it required to close the connection after having called findOne
?
我的理解是,下面的配置定义了多个并发连接,idle是一个参数,使连接管理器关闭空闲的连接:
My understanding is that the following config defines a number of concurrent connections and idle is a parameter making the connection manager closing the connection of idle ones:
module.exports = {
development: {
host: 'db.sqlite',
dialect: 'sqlite',
pool: {
max:5,
min:0,
idle:10000
}
},
test: {
host: 'test.sqlite',
dialect: 'sqlite',
pool: {
max:5,
min:0,
idle:10000
}
}
}
欢迎任何建议
推荐答案
Sequelize 维护一个内部 数据库连接池,这就是 pool
参数的用途,所以这不是必需的.每个调用实际上都是临时借用一个连接,然后在完成后将其返回到池中.
Sequelize maintains an internal database connection pool, that's what the pool
parameters are for, so this isn't necessary. Each call actually borrows a connection temporarily and then returns it to the pool when done.
手动关闭该连接可能会使池中毒并导致性能问题.
Closing that connection manually may poison the pool and cause performance issues.
这篇关于在 Sequelize 操作之后关闭我的连接是一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!