node.js 如何在 Sequelize.js 中使用数据库连接池
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35525574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use database connections pool in Sequelize.js
提问by Mark A
I need some clarification about what the pool is and what it does. The docs say Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database.
我需要澄清一下池是什么以及它的作用。文档说 Sequelize 将在初始化时设置一个连接池,因此理想情况下您应该只为每个数据库创建一个实例。
var sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mysql'|'mariadb'|'sqlite'|'postgres'|'mssql',
pool: {
max: 5,
min: 0,
idle: 10000
},
// SQLite only
storage: 'path/to/database.sqlite'
});
回答by P Ackerman
When your application needs to retrieve data from the database, it creates a database connection. Creating this connection involves some overhead of time and machine resources for both your application and the database. Many database libraries and ORM's will try to reuse connections when possible, so that they do not incur the overhead of establishing that DB connection over and over again. The poolis the collection of these saved, reusable connections that, in your case, Sequelize pulls from. Your configuration of
当您的应用程序需要从数据库中检索数据时,它会创建一个数据库连接。创建此连接涉及应用程序和数据库的一些时间和机器资源开销。许多数据库库和 ORM 会在可能的情况下尝试重用连接,这样它们就不会产生一遍又一遍地建立数据库连接的开销。这pool是这些保存的、可重用的连接的集合,在您的情况下,Sequelize 从中提取。你的配置
pool: {
max: 5,
min: 0,
idle: 10000
}
reflects that your pool should:
反映您的池应该:
- Never have more than five open connections (
max: 5) - At a minimum, have zero open connections/maintain no minimum number of connections (
min: 0) - Remove a connection from the pool after the connection has been idle (not been used) for 10 seconds (
idle: 10000)
- 永远不要有超过五个打开的连接 (
max: 5) - 至少,打开的连接数为零/不保持最小连接数 (
min: 0) - 在连接空闲(未使用)10 秒后从池中删除连接 (
idle: 10000)
tl;dr: Pools are a good thing that help with database and overall application performance, but if you are too aggressive with your pool configuration you may impact that overall performance negatively.
tl;dr:池是有助于提高数据库和整体应用程序性能的好东西,但是如果您对池配置过于激进,则可能会对整体性能产生负面影响。
回答by Chris Troutner
pool is draining error
池正在排空错误
I found this thread in my search for a Sequalize error was giving my node.js app: pool is draining. I could not for the life of me figure it out. So for those who follow in my footsteps:
我在搜索 Sequalize 错误时发现这个线程给了我的 node.js 应用程序:池正在耗尽。我一生都无法弄清楚。所以对于那些追随我脚步的人:
The issue was that I was closing the database earlier than I thought I was, with the command sequelize.closeConnections(). For some reason, instead of an error like 'the database has been closed`, it was instead giving the obscure error 'pool is draining'.
问题是我使用命令sequelize.closeConnections(). 出于某种原因,不是像“数据库已关闭”这样的错误,而是给出了一个模糊的错误“池正在排空”。
回答by shershen
Seems that you can try to put poolto falseto avoid having the pool bing created. Here is the API details table :http://sequelize.readthedocs.org/en/latest/api/sequelize/
似乎您可以尝试 put pooltofalse以避免创建池bing。这是 API 详细信息表:http: //sequelize.readthedocs.org/en/latest/api/sequelize/
[options.pool={}] Object Should sequelize use a connection pool. Default is true
[options.pool={}] 对象应该续集使用连接池。默认为真

