I'm trying to understand the best way to get the connection to my databases.
At the moment I've got a method which parses the URL (depending on the URL called the application has to connect to a different database, like customer1.example.com will connect to the customer1 database) and calls
ActiveRecord::Base.establish_connection(conn_string)
where conn_string contains the name of the database.
This method (set_db) is called with a
before_filter :set_db
in my Application controller, so basically for each request I get, the URL is parsed and the application try to do an establish_connection.
I was wondering if I can have a connection pool somewhere....do you have any suggestion about that? Is it better to have a Singleton which keep all the connections made and gives back the right one?
Thanks! Roberto
Are the databases on the same server?
I have a application where some of the model objects are from one database and others are from a different database. I override the table_name function to specify the database. Won't work if they are different servers but will work for different databases in the same server.
class xx < ActiveRecord.base
def self.table_name
"otherdatabase.table"
end
It also looks like database pooling may be on the way for an upcomming version of rails.
What's New In Edge Rails
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With