Gregory Worrall
Gregory Worrall

Reputation: 181

Rails application unexpectedly throwing errors

It's such a strange occurrence. I can go to the same page three times, and 1 out of those 3 times, the page will throw an error.

Is this a common thing amongst rails applications? Is this caused by me having an incorrect setting somewhere for caching?

I understand that there isn't much context here, but I feel like it's a common occurrence.

I can't exactly copy paste my entire application.

My error logs read:

ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)):
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:189:in `block in wait_poll'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:180:in `loop'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:180:in `wait_poll'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:135:in `block in poll'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:145:in `synchronize'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:133:in `poll'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:425:in `acquire_connection'
  activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'

Upvotes: 1

Views: 342

Answers (1)

Richard Peck
Richard Peck

Reputation: 76784

It's not a Rails error:

ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds))

It's an issue with your database connectivity:

  • Trying to load too much data (IE hitting the DB for 1000+ records in seconds)
  • Have an extremely high latency (slow) connection
  • Have too many open database connections on your system (preventing more from loading)

The way to resolve this will be a debug procedure:

-

DB

How are your database(s) hosted?

We use a shared host to store some MYSQL db's for development (we like to keep our setup lean), and have on-site db hosting (same datacenter) for production apps.

A database server is literally just like requesting JSON objects -- SQL delivers "data" in XML, meaning that if you're requesting it from a system other than the localhost, you need to measure how fast/slow it's returning.

Your problem might be that your system is taking too long to connect to your DB, thus refreshing might be preventing it from accessing it again. The only way to fix this is to get a setup which is geographically close...

enter image description here

We've had this problem with Heroku before -- using external MYSQL db's slowed the system down massively :)

Using something like RackSpace allows you to connect to database servers located in the same datacenter...

enter image description here

-

Dataset

Following on from the above, the second issue you may have is that you're calling massive amounts of data.

Remember, a db server is just a computer, answering your request with data.

As such, if you're calling reams of data in one call, the time required to process it will be substantial (preventing further requests from being handled).

You need to be realistic with your data requests - we tend to stick to perhaps 100 objects in one call, and you have to avoid n+1 queries like the plague.

-

Pool Size

Finally, the pool size of the database connections is another point of consideration.

You can read about this here: https://devcenter.heroku.com/articles/concurrency-and-database-connections

In short, it should be the case that you're limiting your db connections to only the ones which your app needs. Any superfluous ones will just be causing issues.

Upvotes: 4

Related Questions