当连接池耗尽时会发生什么?

2024-04-20 04:00:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读SQLAlchemy的连接池,它的默认连接数为5,默认情况下将溢出到10。

如果超过缓存连接数,会发生什么情况?后续的请求是否排队,直到一个可用的连接可用,或者是否将创建一个不进入池的新连接?

另外,当池“溢出”到默认的最大值10时,未使用的连接会发生什么情况?这些连接是在默认时间之后断开连接(与标准池一样),还是比标准池更积极地释放它们?


Tags: 标准sqlalchemy时间情况排队当池
2条回答

SQLAlchemy docs

When the number of checked-out connections reaches the size set in pool_size, additional connections will be returned up to this limit. When those additional connections are returned to the pool, they are disconnected and discarded. It follows then that the total number of simultaneous connections the pool will allow is pool_size + max_overflow, and the total number of “sleeping” connections the pool will allow is pool_size.

所以是的,溢出的连接比正常休眠的连接更容易被释放。

如果您实际查看QueuePool._do_get()的源,您将看到当连接计数等于池大小+溢出时,它会引发一个TimeoutError,并且在调用connect()后不久不会返回到池的连接。

您正在阅读有关QueuePool的信息,它管理数据库连接以获得更好的性能。它通过保持打开的空闲连接来实现这一点,以备以后重用。它将保持打开的连接数为池大小=5(默认值)。如果打开第六个连接,队列中的一个连接将关闭,只要它处于空闲状态。如果没有空闲,则队列池将打开其他队列池,最大溢出=10(默认值)。如果再这样下去,你就会得到一个错误。 但是,这两个参数都是可配置的。将池大小设置为0以拥有无限打开的连接。 The source is here

相关问题 更多 >