简体   繁体   English

rails 数据库连接池的工作原理

[英]How rails database connection pool works

I am learning rails database connection pool concept.我正在学习 Rails 数据库连接池的概念。 In rails application I have defined pool size of 5.在 Rails 应用程序中,我将池大小定义为 5。

my understanding about connection pool size is as below.我对连接池大小的理解如下。

  1. When server start rails automatically creates n number of connection defined in the database.yml file.当服务器启动时,rails 会自动创建在 database.yml 文件中定义的 n 个连接。 In my case it will create 5 connection since pool size is 5.在我的情况下,它将创建 5 个连接,因为池大小为 5。

  2. On every http request if there is need to access database then rails will use available connection from the connection pool to serve the request.在每个 http 请求上,如果需要访问数据库,那么 rails 将使用连接池中的可用连接来为请求提供服务。

But my question is if I hit 1000 request at a time then most of the request will not get access to database connection because my connection pool size is only 5.但我的问题是,如果我一次达到 1000 个请求,那么大部分请求将无法访问数据库连接,因为我的连接池大小只有 5。

Is my above understanding about rails connection pool is right??我上面对rails连接池的理解是对的吗??

Thanks,谢谢,

Purpose:目的:
Database connections are not thread safe;数据库连接不是线程安全的; so ActiveRecord uses separate database connection for each thread.所以 ActiveRecord 为每个线程使用单独的数据库连接。

Limiting factor:限制因素:
Total database connections is limited by the database server you use (eg Posgres: default is typically 100 or lesser ), by your app server's configuration (number of processes/threads available) and Active Record's configuration : Connection Pool defaults to 5 .数据库连接总数受您使用的数据库服务器(例如 Posgres: 默认通常为 100 或更少)、应用服务器的配置(可用进程/线程数)和 Active Record 的配置的限制: 连接池默认为 5 。

Pool size:泳池大小:
Active Record's pool size is for a single process. Active Record 的池大小适用于单个进程。 A thread uses a connection from this pool and releases it automatically afterwards.一个线程使用这个池中的一个连接并在之后自动释放它。 (unless you spawn a thread yourself, then you'll have to manually release it). (除非您自己生成一个线程,否则您必须手动释放它)。 If your application is running on multiple processes, you will have 5 database connections for each of them.如果您的应用程序在多个进程上运行,则每个进程都有 5 个数据库连接。 If your server is hit by 1000 requests concurrently, it will distribute the requests among these connections, when it gets full, rest of the requests wait for their turn.如果您的服务器同时收到 1000 个请求,它将在这些连接之间分配请求,当它已满时,其余的请求会等待轮到它们。

Read more at:阅读更多信息:
https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html

Yes, from the docs:是的,来自文档:

A connection pool synchronizes thread access to a limited number of database connections.连接池将线程访问同步到有限数量的数据库连接。 The basic idea is that each thread checks out a database connection from the pool, uses that connection, and checks the connection back in. ConnectionPool is completely thread-safe, and will ensure that a connection cannot be used by two threads at the same time, as long as ConnectionPool's contract is correctly followed.基本思想是每个线程从池中检出一个数据库连接,使用该连接,然后检入该连接。 ConnectionPool 是完全线程安全的,将确保一个连接不能被两个线程同时使用,只要正确遵循 ConnectionPool 的合同。 It will also handle cases in which there are more threads than connections: if all connections have been checked out, and a thread tries to checkout a connection anyway, then ConnectionPool will wait until some other thread has checked in a connection.它还将处理线程数多于连接数的情况:如果所有连接都已签出,并且一个线程无论如何都试图签出一个连接,那么 ConnectionPool 将等待某个其他线程签入一个连接。

source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html来源: http : //api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html

If you use something like unicorn as http server:如果你使用像 unicorn 这样的东西作为 http 服务器:

In Unicorn each process establishes its own connection pool, so you if your db pool setting is 5 and you have 5 Unicorn workers then you can have up to 25 connections.在 Unicorn 中,每个进程都建立自己的连接池,因此如果您的数据库池设置为 5 并且您有 5 个 Unicorn 工作人员,那么您最多可以拥有 25 个连接。 However, since each unicorn worker can handle only one connection at a time, then unless your app uses threading internally each worker will only actually use one db connection.但是,由于每个 unicorn worker 一次只能处理一个连接,除非您的应用程序在内部使用线程,否则每个 worker 实际上只会使用一个 db 连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM