Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to list all database connections currently in the pool?

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.

like image 901
Erik B Avatar asked Nov 14 '11 18:11

Erik B


People also ask

How do I see how many connections I have in connection pool?

This is in the "bin" folder. connect - You may need to enter servername/port/user/password depending on how your server is configured. Once you are connected, the command to grab the connection information will be something like. The part which tells you the number of live connections is listed as "Active Count".

How do I monitor my database connection pool?

To see connection pooling information, you should install the Java M-Beans plugin for your version of VisualVM. See the VisualVM documentation for more information on M-Beans plugin. After you install the plugin, in the MBeans tab, you can identify the numbers of connections used in the connection pool.

Is database connection pool?

What is database connection pooling? Database connection pooling is a way to reduce the cost of opening and closing connections by maintaining a “pool” of open connections that can be passed from database operation to database operation as needed.


1 Answers

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

like image 61
ErsatzRyan Avatar answered Nov 08 '22 01:11

ErsatzRyan