3

I'm using ActiveRecord in a Ruby script, without using rails, just running the Ruby script. The script kicks off threads which access the database. Eventually, I get:

could not obtain a database connection within 20.000 seconds

database configuration:
The pool is set for 10 connection.  The timeout is set for 20 seconds.

I tried without using connection poll calls directly, but I'm not clear on how that is supposed to be managed. So I put cxn = ActiveRecord::Base.connection_pool.checkout and ActiveRecord::Base.connection_pool.checkin(cxn) around the database access portions of the code. I still get the error.

I put some debug in the script, and I can see the checkout/checkin calls happening. There are 7 successful checkout/checkins. There are 13 total checkouts, so 6 left open.

I'm also seeing:

undefined method `run_callbacks'

when the timeout occurs.

Thank you, Jerome

1
  • Hey Jerome, it would help if you edited your question to include a direct question. Commented Jun 12, 2014 at 15:35

1 Answer 1

3

You need to explicitly release connections back to the ActiveRecord connection pool. You either need to explicitly call ActiveRecord::Base.clear_active_connections! or else define a helper method which does the following:

def with_connection(&block)
  ActiveRecord::Base.connection_pool.with_connection do
    yield block
  end
end

which you would use as follows:

def my_method

  with_connection do
    User.where(:id => 1)
  end
end

which should release your connection when the block exits.

Be warned that ActiveRecord uses the current Thread ID to manage connections, so if you are spawning off threads each thread needs to release connections once done with them. Your Threads are most likely not clearing connections properly, or you are spawning more threads than you have connections available in your connection pool.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.