8

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.

Erik B
  • 40,889
  • 25
  • 119
  • 135

2 Answers2

6

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

ErsatzRyan
  • 3,033
  • 1
  • 26
  • 30
  • I believe it has its own because it is running in a separate process, however I do not know how you are running your daemon your DB might limit connections thus refusing your daemon connections – ErsatzRyan Nov 22 '11 at 00:40
0

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)

olingern
  • 494
  • 1
  • 4
  • 15