简体   繁体   中英

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM