Mark Thomas Miller's logo

Fixing Apartment's `ActiveRecord::ConnectionNotEstablished`

September 10, 2018

I've run into this problem when using the Apartment gem for Ruby on Rails 5.2. If subdomain.lvh.me:3000 is taking forever to load and you're seeing verbose console logs starting with ActiveRecord::ConnectionNotEstablished (No connection pool with ‘primary’ found), there's a threading issue happening with your server. This is how to fix it:

  1. Inside puma.rb, find the line:

    threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
    

    And change it to:

    threads_count = 1
    
  2. Then restart your server. If you're running Apartment and want to use subdomains with lvh.me, you'll want to restart your server with the correct parameters:

    rails s -p 3000 -b lvh.me
    

I haven't encountered this error in production (where I'm on Heroku and using PostgreSQL as my database), so it might pertain to development environments only. However, it's still annoying if you're trying to develop an application.

If you're here from my step-by-step Apartment tutorial:

How to set up Apartment for multi-tenant Rails apps