title: Would it be possible to have multiple database connection pools in rails to switch between?
body: <p><strong>A little background</strong></p>
  
  <p>I have been using the <a href="https://github.com/influitive/apartment" rel="noreferrer">Apartment gem</a> for running a multi-tenancy app for years. Now recently the need to scale the database out into separate hosts has arrived, the db server simply can't keep up any more (both reads and writes are getting too too much) - and yes, I scaled the hardware to the max (dedicated hardware, 64 cores, 12 Nvm-e drives in raid 10, 384Gb ram etc.). </p>
  
  <p>I was considering doing this per-tenant (1 tenant = 1 database connection config / pool) as that would be a "simple" and efficient way to get up to <code>number-of-tenants</code>-times more capacity without doing loads of application code changes. </p>
  
  <p>Now, I am running rails 4.2 atm., soon upgrading to 5.2. I can see that rails 6 adds support for a per-model connection definitions, however that is not really what I need, as I have a completely mirrored database schema for each of my 20 tenants. Typically I switch "database" per request (in middleware) or per background job (sidekiq middleware), however this is currently trivial and handled ny the Apartment gem, as it just sets the <code>search_path</code> in Postgresql and does not really change the actual connection. When switching to a per-tenant hosting strategy I will need to switch the entire connection per request.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>I understand that I could do an <code>ActiveRecord::Base.establish_connection(config)</code> per request / background job - however, as I also understand, that triggers an entirely new database connection handshake to be made and a new db pool to spawn in rails - right? I guess that would be a performance suicide to make that kind of overhead on every single request to my application.</li>
  <li>I am therefore wondering if anyone can see the option with rails of e.g. pre-establishing multiple (total of 20) database connections/pools from the beginning (e.g. on boot of the application), and then just switch between those pools per request? So that he db connections are already made and ready to be used.</li>
  <li>Is all this just a poor poor idea, and should I instead look for a different approach? E.g. 1 app instance = one specific connection to one specific tenant. Or something else. </li>
  </ol>