chiborg
chiborg

Reputation: 28144

Ways around ActiveRecord connection pool

I'm writing an Adhearsion component that uses ActiveRecord. The problem is that the component may be running for several minutes (the length of a call). During that time the component has an ActiveRecord object as an instance variable. This object uses up one database connection from the connection pool. Depending on the number of callers, the connection pool may be exhausted prematurely. The data is saved several times during the call, but does not need to be available instantly. While using ActiveRecord is very convenient, it's not needed. I can imagine several solutions and would like to ask for opinions, tips and alternative solutions from the community:

I don't like the solutions that need external software running (message queue, web service).

Upvotes: 1

Views: 3108

Answers (2)

Ben Klang
Ben Klang

Reputation: 306

We have addressed this question in a blog post on our website: http://mojolingo.com/blog/2011/scaling-activerecord-in-adhearsion/

The blog post explains the problem you were seeing along with one possible solution to fix it. We have also released a gem to go with it that implements the described solution: https://github.com/adhearsion/activerecord-wrap-with-connection

We are rethinking the way ActiveRecord will work with Adhearsion 2.0, so this gem should not be necessary in the future.

Upvotes: 1

eric
eric

Reputation: 1197

Adhearsion currently does not clean up checked out ActiveRecord connections the way it should.

If you call ActiveRecord::Base.connection_pool.release_connection in an ensure block at the end of your dialplan, you can make sure connections are not held on to any longer than they need to be.

This should mean that your connection pool only needs to be as big as the number of simultaneous calls you're handling.

Upvotes: 0

Related Questions