为单线程Rails应用程序正确设置数据库连接池database.yml [英] Correct setting of database connection pool database.yml for single-threaded Rails applications

查看:468
本文介绍了为单线程Rails应用程序正确设置数据库连接池database.yml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Rails database.yml中的以下设置:

I was wondering about the following setting in the Rails database.yml:

默认情况下,ActiveRecord的连接池的数据库连接数设置为5:

By default the number of database connections for the connection pool of ActiveRecord is set to 5:

development:
  ...
  pool: 5

但是默认情况下,Rails 3是单线程的。
为什么你默认需要5个连接?

But by default, Rails 3 is single threaded. Why would you need 5 connections by default?

据我所知,单线程Rails应用程序不能同时触发多个数据库操作,为什么你会需要保持更多的连接打开吗?

As far as I understand, a single threaded Rails app can't trigger multiple database operations at once, why would do you need to keep more connection open?

我假设2个连接是有意义的,所以你总是有一个活动连接,即使另一个超时,

I would assume that 2 connections would make sense, so you always have one active connection even if the other one times out, but holding five connections seems a little odd to me.

我错过了什么?

UPDATE
如果其他人很好奇,我只是发现了一个解释它的提交:
https://github.com/ rails / rails / commit / b700153507b7d539a57a6e3bcf03c84776795051

事实上,这些默认设置没有任何意义,它被修复,但暂时还原(一年前)因为测试套件。

In fact these default settings don't make any sense, it was fixed but then temporarily reverted (one year ago) because of the test suite.

推荐答案

这里的晚会很晚,但我今天在生产中跑出了数据库连接。

Quite late to the party here, but I ran out of database connections today in production.

像许多人一样,我使用Sidekiq执行异步作业,例如发送电子邮件。重要的是注意Sidekiq作为一个多线程进程运行。

Like a lot of people, I use Sidekiq to perform asynchronous jobs like sending emails for example. It is important to note that Sidekiq runs as a multithread process.

所以,我不只是有一个单线程Rails应用程序,因此这个答案不直接适用于问题,但我认为这里值得说的东西,因为我认为多线程Rails应用程序是相对正常的。

So, I don't just have a single-threaded Rails application, therefore this answer does not directly apply to the question asked but I thought it was worth saying something here as I think multithreaded Rails apps are relatively normal nowadays.

这意味着您需要调整池大小,以创建足够的连接来处理所有可以入队的作业,并花费超过5秒钟在抛出错误之前等待数据库连接的默认超时时间段)。

This means you need to adjust your pool size in such a way as to create enough connections to handle all the jobs that can be enqueued and take longer than 5 seconds (the default timeout period to wait for a database connection before throwing an error).

这篇关于为单线程Rails应用程序正确设置数据库连接池database.yml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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