Open Gokul-Edcast opened 5 months ago
Thanks for your report! I haven't read through this in-depth, but I know that we use a read-replica Postgres along with our Postgres primary without issue in Apartment. I'm attending a conference this week so it may be some time before I can take a closer look. @npezza93 or @mrpasquini do you have any thoughts?
Thanks @mnovelo for the response, This is happening when we switch replica database multiple times.
def switch_to_tenant(tenant_name, replica_db = nil)
Apartment::Tenant.switch!(tenant_name)
if tenant_name.present? && replica_db.present? && RequestStore.read(:read_replica_enabled)
ActiveRecord::Base.connects_to(database: {reading: replica_db.to_sym })
end
end
This is the method we used to switch the primary as well as replica database
ActiveRecord::Base.connects_to(database: {reading: replica_db.to_sym })
This line is causing the issue.
Interesting. I'm not as familiar with the MySQL implementation, but I know for Postgres it's more optimal to do the tenant switch after the db switch because the tenant switch issues a SET search_path=
on the current connection, so it's needless work for us to switch tenants on the current connection if we're switching databases on the next line. Have you tried moving the Apartment::Tenant.switch!(tenant_name)
to be after your db switch?
Can you share your Sample Postgres code here?
@sunil-sharma what do you mean?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
We are implementing multi-db on our app with help of ros-apartment gem. In our application we are using primary db as well as replica db for reading. We are switching write db with help of ros-apartment gem(_Apartment::Tenant.switch!(tenantname)) and using read db with help of
ActiveRecord::Base.connects_to(database: {reading: replica_db.to_sym })
. Loading the read replica db config dynamically using an initialiser file.app/config/initializers/db_config_load.rb
`
In some place in our code we are using reading role to read data from read replica.
ApplicatioRecord.rb
connects_to database: { writing: :primary, reading: :primary_replica }
Connection Pool for the replica_db is 25 When we hit multiple time on the api's we got the below error
No connection pool for 'ActiveRecord::Base' found for the 'reading' role.
In our mysql rds instance we have around 650 connections and it is using only 190 pools but still we got the connection pool error.Guys any idea why the connection pool error, is it because of db config issue? or ros-aprtment tenant switch using all the connection pool? How ros-apartment gem is handling connection pool in rails?
System configuration
Database: mysql Ver 14.14 Distrib 5.7.44, for osx10.17 (x86_64) using EditLine wrapper
Apartment version: ros-apartment (2.11.0)
Apartment config (in
config/initializers/apartment.rb
or so):use_schemas
: (true
orfalse
)Rails (or ActiveRecord) version: Rails. 7.0
Ruby version: Ruby : 3.0.6