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).
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.