rails 数据库连接池的工作原理
How rails database connection pool works
我正在学习 rails 数据库连接池概念。在 rails 应用程序中,我将池大小定义为 5。
我对连接池大小的理解如下。
当服务器启动时rails自动创建n个在database.yml文件中定义的连接。在我的例子中,它将创建 5 个连接,因为池大小为 5。
在每个 http 请求上,如果需要访问数据库,那么 rails 将使用连接池中的可用连接来处理请求。
但我的问题是,如果我一次达到 1000 个请求,那么大部分请求将无法访问数据库连接,因为我的连接池大小只有 5 个。
我上面对rails连接池的理解对吗??
谢谢,
是的,来自文档:
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.
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.
来源:http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html
如果您使用 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. 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.
目的:
数据库连接不是线程安全的;所以 ActiveRecord 为每个线程使用单独的数据库连接。
限制因素:
数据库连接总数受您使用的数据库服务器的限制(例如 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 默认为 5 .
池大小:
Active Record 的池大小适用于单个进程。线程使用此池中的连接并在之后自动释放它。 (除非您自己生成一个线程,否则您必须手动释放它)。如果您的应用程序在多个进程上 运行,则每个进程将有 5 个数据库连接。如果您的服务器同时受到 1000 个请求的攻击,它将在这些连接之间分配请求,当它满了时,其余的请求等待轮到它们。
阅读更多内容:
https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html
我正在学习 rails 数据库连接池概念。在 rails 应用程序中,我将池大小定义为 5。
我对连接池大小的理解如下。
当服务器启动时rails自动创建n个在database.yml文件中定义的连接。在我的例子中,它将创建 5 个连接,因为池大小为 5。
在每个 http 请求上,如果需要访问数据库,那么 rails 将使用连接池中的可用连接来处理请求。
但我的问题是,如果我一次达到 1000 个请求,那么大部分请求将无法访问数据库连接,因为我的连接池大小只有 5 个。
我上面对rails连接池的理解对吗??
谢谢,
是的,来自文档:
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. 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.
来源:http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html
如果您使用 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. 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.
目的:
数据库连接不是线程安全的;所以 ActiveRecord 为每个线程使用单独的数据库连接。
限制因素:
数据库连接总数受您使用的数据库服务器的限制(例如 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 默认为 5 .
池大小:
Active Record 的池大小适用于单个进程。线程使用此池中的连接并在之后自动释放它。 (除非您自己生成一个线程,否则您必须手动释放它)。如果您的应用程序在多个进程上 运行,则每个进程将有 5 个数据库连接。如果您的服务器同时受到 1000 个请求的攻击,它将在这些连接之间分配请求,当它满了时,其余的请求等待轮到它们。
阅读更多内容:
https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html