如何在 Sequelize.js 中使用数据库连接池 [英] How to use database connections pool in Sequelize.js

查看:101
本文介绍了如何在 Sequelize.js 中使用数据库连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要澄清一下池是什么以及它的作用.文档说 Sequelize 将在初始化时设置一个连接池,因此理想情况下您应该只为每个数据库创建一个实例.

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.

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'
});

推荐答案

当您的应用程序需要从数据库中检索数据时,它会创建一个数据库连接.创建此连接涉及应用程序和数据库的一些时间和机器资源开销.许多数据库库和 ORM 会在可能的情况下尝试重用连接,这样它们就不会产生一遍又一遍地建立数据库连接的开销.pool 是这些保存的、可重用的连接的集合,在您的情况下,Sequelize 从中提取.您对

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 pool is the collection of these saved, reusable connections that, in your case, Sequelize pulls from. Your configuration of

pool: {
    max: 5,
    min: 0,
    idle: 10000
  }

反映您的池应该:

  1. 打开的连接永远不要超过五个(max: 5)
  2. 至少,打开的连接数为零/不保持最小连接数 (min: 0)
  3. 在连接空闲(未使用)10 秒后从池中删除连接(idle: 10000)

tl;dr:池是有助于提高数据库和整体应用程序性能的好东西,但如果您对池配置过于激进,则可能会对整体性能产生负面影响.

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.

这篇关于如何在 Sequelize.js 中使用数据库连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆