rails-on-services/apartment

ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.) with multiple databases using ROS Apartment

Closed this issue · 2 comments

I have 3 tenants with tenantone, tenanttwo, tenantthree subdomains and 3 different databases,
E.g. Subdomain: tenantone, Database: tenantone URL: tenantone.xyz.com
when I trying to logged in in any of tenant then it gives timeout error:
ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.):

Also sometimes it gives issue:
Apartment::TenantNotFound (Error while connecting to tenant production: could not obtain a connection from the pool within 5.000

Also I am using Postgres database on server. When I make fresh deployment then it start working but when logged in with multiple tenant after some time it stop working and every ajax call fails.

This issue not occurred in development environment. I am trying to solve this issue from last 2 weeks.

Also I am using manual database switch call using Apartment::Tenant.switch!("databasename")
Thanks in advance

Hello @patil-gaurav

I think those of us using multiple database configuration need a separate forum, because I really struggled a lot with getting my app production environment to a stable condition.

Regarding your issues:

  1. Are you running your app on a multithreaded server. e.g. Puma?
  2. If Yes, what is the maximum allowed connections for each of the Tenant database servers?

One of the things I did was to increase the database pool config to for each of my Tenant DB connection based on the app usage/traffic noticed.

From my understanding so far, a lot of database connection pool will be required depending on your app traffic/usage.

For example:
Imagine you have 40 users hitting your app almost at the same time, The Apartment::Tenant.switch!(db) will be called 40 times, which means if there are no DB connection pool available then this error will definitely occur:
ActiveRecord::ConnectionNotEstablished (No connection pool with 'primary' found.):

I am opened for more discussions/corrections

@inspiredstuffs any update on this?