I’m running rails 7.0, and having a bit of trouble configuring the pool and timeout for one of the databases the application needs.
My database.yml file looks like this:
development:
(my dev env settings here settings here)
production:
adapter: <%= ENV["PROD_DB_ADAPTER"] %>
host: <%= ENV["PROD_DB_HOST"] %>
database: <%= ENV["PROD_DB_DB"] %>
username: <%= ENV["PROD_DB_USERNAME"] %>
password: <%= ENV["PROD_DB_PASSWORD"] %>
port: <%= ENV["PROD_DB_PORT"] %>
pool: 5
timeout: 5000
my_other_database:
adapter: <%= ENV["MY_ADAPTER"] %>
host: <%= ENV["MY_HOST"] %>
database: <%= ENV["MY_DB"] %>
username: <%= ENV["MY_USERNAME"] %>
password: <%= ENV["MY_PASSWORD"] %>
port: <%= ENV["MY_PORT"] %>
pool: 20
timeout: 10000
I have confirmed the ENV variables are passing the right values
One of my rails models needs to connect to “my_other_database”, which is different from the database that the app uses, and I have that setup as follows:
class MyModel < ActiveRecord::Base
establish_connection(:my_other_database)
end
According to what I see in the documentation, that is the way to set it:
https://apidock.com/rails/v7.0.0/ActiveRecord/ConnectionHandling/establish_connection
But when I run my application and try to use that model to pull data, I get the following error:
No connection pool for ‘MyModel’ found.
If I change my model to look like this (according to that same documentation):
class MyModel < ActiveRecord::Base
establish_connection(
adapter: 'adapter here',
host: 'host here,
database: 'database name here',
username: 'username here',
password: 'password here',
port: 'port here',
pool: 20, (notice these two are not part of the documentation, but I just don't know how else to try them)
timeout: 1000 (same as the other above)
)
end
Setting the model that way (almost always) works… with high traffic, it seems to timeout sometimes, with this error:
could not obtain a connection from the pool within 5.000 seconds (waited 5.007 seconds); all pooled connections were in use
Notice the “within 5.000 seconds” part. It seems it has ignored my “timeout: 10000”, and fall back to the default
So I am a bit confused. Why would establish_connection(:my_other_database) doesn’t just work as advertised in the docs?
And why if I do it the other way, it just ignores pool and timeout, and falls back to the defaults?
Is there a way I could force pool and timeout to what I need them to be in that establish_connection block some other way?
Please help!