views:

51

answers:

1

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:

  • Have a very big connection pool (> 1000 connections).
  • Claim and release a database connection each time when my ActiveRecord object is changed. Don't know how to do that, code examples would be appreciated.
  • Write data to a log file that is imported in regular intervals into the database.
  • Set up a web service on separate server process that accepts data through HTTP request and writes it to the DB.
  • Use a message queue where the data gets sent to. A worker imports the data to the DB.

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

A: 

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.

eric
And that's the problem: It's a component for test purposes that generates a very high number of simultaneous calls.
chiborg
It generates more than 1000 calls? Depending on your call flow, you can put additional calls to `ActiveRecord::Base.connection_pool.release_connection` where you will have a long pause in your dialplan. ActiveRecord will automatically get a new connection from the pool the next time you need one.
eric