是否可以列出当前池中的所有数据库连接?

发布于 2024-12-16 10:37:59 字数 578 浏览 2 评论 0原文

我在独立于 Rails 应用程序运行的守护进程中收到 ActiveRecord::ConnectionTimeoutError 。我使用 Passenger 和 Apache 和 MySQL 作为数据库。

Passenger 的默认池大小为 6(至少文档是这么告诉我的),因此它不应使用超过 6 个连接。

我已将 ActiveRecord 的池大小设置为 10,尽管我认为我的守护进程只需要一个连接。我的守护进程是一个具有多个线程的进程,它到处调用 ActiveRecord 将内容保存到与 Rails 应用程序共享的数据库中。

我需要弄清楚的是,线程是否根本无法共享一个连接,或者它们是否只是不断请求新连接而不释放旧连接。我知道我可以增加池大小并推迟问题,但是守护进程可以有数百个线程,池迟早会耗尽连接。

我想知道的第一件事是 Passenger 确实只使用了 6 个连接,问题出在守护进程上。我该如何测试呢?

其次,我想弄清楚是否每个线程都需要自己的连接,或者是否只需要告诉它们重用它们已有的连接。如果他们确实需要自己的联系,也许他们只需要被告知在不使用它们时不要保留它们?毕竟,线程大部分时间都在休眠。

I'm getting ActiveRecord::ConnectionTimeoutError in a daemon that runs independently from the rails app. I'm using Passenger with Apache and MySQL as the database.

Passenger's default pool size is 6 (at least that's what the documentation tells me), so it shouldn't use more than 6 connections.

I've set ActiveRecord's pool size to 10, even though I thought that my daemon should only need one connection. My daemon is one process with multiple threads that calls ActiveRecord here and there to save stuff to the database that it shares with the rails app.

What I need to figure out is whether the threads simply can't share one connection or if they just keep asking for new connections without releasing their old connections. I know I could just increase the pool size and postpone the problem, but the daemon can have hundreds of threads and sooner or later the pool will run out of connections.

The first thing I would like to know is that Passenger is indeed just using 6 connections and that the problem lies with the daemon. How do I test that?

Second I would like to figure out if every thread need their own connection or if they just need to be told to reuse the connection they already have. If they do need their own connections, maybe they just need to be told to not hold on to them when they're not using them? The threads are after all sleeping most of the time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白鸥掠海 2024-12-23 10:37:59

您可以通过 ActiveRecord::Base.connection_handler.connection_pools 获取 ActiveRecord 正在使用的连接池,它应该是一个连接池数组。您可能只有一个,并且它有一个 connections 方法。获取它所知道的连接数组。

您还可以执行 ActiveRecord::Base.connection_handler.connection_pools.each(&:clear_stale_cached_connections!) ,它将签入任何线程不再活动的已签出连接。

不知道这是否有帮助或让更多人感到困惑

You can get to the connection pools that ActiveRecord is using through ActiveRecord::Base.connection_handler.connection_pools it should be an array of connection pools. You probably will only have one in there and it has a connections method on it. To get an array of connections it knows about.

You can also do a ActiveRecord::Base.connection_handler.connection_pools.each(&:clear_stale_cached_connections!) and it will checkin any checked out connections which thread is no longer alive.

Don't know if that helps or confuses more

幻梦 2024-12-23 10:37:59

自 2019 年 2 月起,clear_state_cached_connections 已弃用并移至 reap

提交

更新了先前接受的答案:

ActiveRecord::Base.connection_handler.connection_pools.each(&:reap)

As of February 2019, clear_state_cached_connections has been deprecated and moved to reap

Commit

Previous accepted answer updated:

ActiveRecord::Base.connection_handler.connection_pools.each(&:reap)

近箐 2024-12-23 10:37:59
ActiveRecord::Base.connection_pool.stat

它会回来的。

{:size=>10, :connections=>3, :busy=>1, :dead=>0, :idle=>2, :waiting=>0, :checkout_timeout=>5.0}

如果您对多线程数据库工作感兴趣,请在此处查看我的答案。

ActiveRecord::Base.connection_pool.stat

It will return.

{:size=>10, :connections=>3, :busy=>1, :dead=>0, :idle=>2, :waiting=>0, :checkout_timeout=>5.0}

If you interested in Multi-Threaded DB work, Please check my answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文