Can I list all database connections currently in the pool?

I get ActiveRecord::ConnectionTimeoutError in a daemon that works regardless of the rails application. I am using Passenger with Apache and MySQL as a database.

The default pool size for passengers is 6 (at least what the documentation tells me), so it should not use more than 6 connections.

I set the ActiveRecord pool size to 10, although I thought that my daemon needed only one connection. My daemon is a single process with multiple threads, which calls ActiveRecord here and there to store material in a database that it shares with the application rails.

What I need to find out is that the threads simply cannot share one connection, or if they just continue to request new connections without letting go of their old connections. I know that I could just increase the size of the pool and postpone the problem, but the daemon can have hundreds of threads and sooner or later the pool will work out of connections.

The first thing I would like to know is that Passenger really only uses 6 connections and that the problem is the daemon. How to check it?

Secondly, I would like to find out if each thread needs its own connection or just need to repeat the use of an existing connection. If they need their own connections, maybe they just need to tell them not to hold onto them when they don't use them? Threads sleep most of all most of the time.

+6
source share
2 answers

You can get the connection pools that ActiveRecord uses with ActiveRecord::Base.connection_handler.connection_pools , this should be an array of connection pools. You will probably only have one, and it has a connections method on it. To get the array of connections he knows about.

You can also do ActiveRecord::Base.connection_handler.connection_pools.each(&:clear_stale_cached_connections!) And it will check all verified connections whose stream is no longer alive.

I don't know if this helps more or less.

+5
source

As of February 2019, clear_state_cached_connections deprecated and moved reap

commit

Previously accepted answer updated:

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

0
source

Source: https://habr.com/ru/post/1381180/


All Articles