Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple database connection

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

like image 403
Roberto Avatar asked Nov 14 '22 17:11

Roberto


1 Answers

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

like image 64
Ken Avatar answered Dec 07 '22 23:12

Ken