为什么要限制SQLAlchemy中的DB连接池大小?

时间:2022-04-23 17:00:28

This is a follow-up to a question I posted earlier about DB Connection Pooling errors in SQLAlchemy.

这是我之前发布的关于SQLAlchemy中DB连接池错误的问题的后续跟进。

According to the SQLAlchemy docs the sqlalchemy.pool.QueuePool.__init__() method takes the following argument:

根据SQLAlchemy文档,sqlalchemy.pool.QueuePool .__ init __()方法采用以下参数:

pool_size – The size of the pool to be maintained, defaults to 5. This is the largest number of connections that will be kept persistently in the pool. Note that the pool begins with no connections; once this number of connections is requested, that number of connections will remain. pool_size can be set to 0 to indicate no size limit; to disable pooling, use a NullPool instead.

pool_size - 要维护的池的大小,默认为5.这是将在池中持久保留的最大连接数。请注意,池开始时没有连接;一旦请求此连接数,将保留该连接数。 pool_size可以设置为0表示没有大小限制;要禁用池,请改用NullPool。

What are the drawbacks to setting pool_size=0? What is the benefit of limiting the connection pool size? Is it just to save memory? The database shouldn't really care if a large number of unused connections are open, right?

设置pool_size = 0有什么缺点?限制连接池大小有什么好处?它只是为了节省内存吗?如果大量未使用的连接打开,数据库应该不在乎,对吧?

1 个解决方案

#1


5  

The main drawback to not limiting the pool size would be the potential for a runaway program to create too many connections.

不限制池大小的主要缺点是失控程序可能会创建太多连接。

There are real limits, both at the database level, and O/S level to how many connections the database will support. Each connection will also use additional memory. Limiting the pool size helps protect your database against both bugs in your program, or a malicious attack on your server. Either of those could bring your database server to a standstill by using too many connections or too much memory.

数据库级别和O / S级别对数据库将支持的连接数有实际限制。每个连接也将使用额外的内存。限制池大小有助于保护数据库免受程序中的错误或服务器上的恶意攻击。这些中的任何一个都可能通过使用太多连接或太多内存使数据库服务器停滞不前。

Under normal circumstances, the additional memory each connection uses shouldn't be too much of an issue, but it's best to limit it to the maximum number you think you'll use concurrently (maybe plus a few for good measure).

在正常情况下,每个连接使用的额外内存不应该是一个太大的问题,但最好将其限制为您认为将同时使用的最大数量(可能加上一些用于良好测量)。

#1


5  

The main drawback to not limiting the pool size would be the potential for a runaway program to create too many connections.

不限制池大小的主要缺点是失控程序可能会创建太多连接。

There are real limits, both at the database level, and O/S level to how many connections the database will support. Each connection will also use additional memory. Limiting the pool size helps protect your database against both bugs in your program, or a malicious attack on your server. Either of those could bring your database server to a standstill by using too many connections or too much memory.

数据库级别和O / S级别对数据库将支持的连接数有实际限制。每个连接也将使用额外的内存。限制池大小有助于保护数据库免受程序中的错误或服务器上的恶意攻击。这些中的任何一个都可能通过使用太多连接或太多内存使数据库服务器停滞不前。

Under normal circumstances, the additional memory each connection uses shouldn't be too much of an issue, but it's best to limit it to the maximum number you think you'll use concurrently (maybe plus a few for good measure).

在正常情况下,每个连接使用的额外内存不应该是一个太大的问题,但最好将其限制为您认为将同时使用的最大数量(可能加上一些用于良好测量)。