单线程的Rails应用程序

单线程的Rails应用程序

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

问题描述

我想知道在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

不过,在默认情况下,轨道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.

我缺少的东西?

更新如果任何人是好奇,我刚刚发现了一个承诺,解释它:https://github.com/rails/rails/commit/b700153507b7d539a57a6e3bcf03c84776795051

UPDATEIf anyone else is curious, I just found a commit that explains it: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.

推荐答案

连接池像杂种/乘客/等单线程服务器的主要好处是,连接建立/保持在一个机架处理程序之外的主轨要求加工。这允许,因为它是用不同的方式来建立一次与多次的连接。我们的目标是重复使用所建立的连接,并尽量减少连接的数目。这应该prevent其请求之间给定的请求处理周期内重新连接,甚至可能(如果我没有记错)。

Manage Connections

The major benefit of connection pooling for a single-thread server like Mongrel/Passenger/etc is that the connection is established/maintained in a Rack handler outside the main Rails request processing. This allows for a connection to be established once vs. many times as it's used in different ways. The goal is to re-use the established connection and minimize the number of connections. This should prevent having to reconnect within a given request processing cycle and possibly even between requests (if I recall correctly).

虽然大多数用例(杂种/乘客)是单线程的,只能使用一次的单一连接 - 有JRuby和环境具有完全多线程的支持/应用服务器。因为2.2的Rails已经线程安全的。

Although most use cases (Mongrel/Passenger) are single threaded and can only use a single connection at a time - there is JRuby and environments/app servers that have full multi-threaded support. Rails has been thread safe since 2.2

  • 连接池是ActiveRecord的内部处理的,因此所有应用程序服务器应该表现基本相同。

  • Connection pooling is handled inside of ActiveRecord, so all application servers should behave basically the same.

数据库连接池开始是空的,并创建根据需求在一段时间的连接。这个池默认的最大大小为5,配置在database.yml中。

The database connection pool starts out empty and creates connections over time according to demand. The maximum size of this pool defaults to 5 and is configured in database.yml.

请求,并从该池的用户共享连接。请求检查出一个连接在第一时间,它需要访问数据库,然后检查连接回在请求的结束。

Requests and users share connections from this pool. A request checks out a connection the first time it needs to access the database and then checks the connection back in at the end of the request.

如果您使用Rails.threadsafe!模式,那么多线程可能会访问的同时多个连接,所以根据请求负载,你可能有多个线程争几连接。

If you use Rails.threadsafe! mode, then multiple threads might beaccessing multiple connections at the same time, so depending on therequest load you might have multiple threads contending for a fewconnections.

您可以相应地改变,如果你使用的是单线程应用程序。默认值是5按照大多数用户的需要,因为现在每天的正常的,有一个多线程的应用程序。

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

08-13 17:12
查看更多