Multiple database connections

I'm trying to figure out how best to get a connection to my databases.

Right now I have a method that parses the URL (depending on the URL that the application should connect to another database, for example client1.example.com will connect to customer1 database) and calls

ActiveRecord::Base.establish_connection(conn_string)

where conn_string contains the database name.

This method (set_db) is called using

before_filter :set_db

in my application controller, so basically for every request I receive, the URL is parsed and the application is trying to set connection_connection.

I was wondering if I have a connection pool somewhere ... do you have any suggestions on this? Is it better to have a singleton that supports all connections and returns the correct one?

Thanks! Roberto

+5
source share
4 answers

Are databases on a single server?

I have an application in which some of the model objects from one database and others from another database. I override the table_name function to indicate the database. It does not work if they are different servers, but will work for different databases on the same server.

class xx < ActiveRecord.base

def self.table_name
  "otherdatabase.table"
end

It looks like the database pool might be on the way for an advanced version of rails.

What's New in Edge Rails

+1
source

I'm not a Ruby programmer, but generally speaking, a connection pool is a good idea. You can make this connection pool a single and transfer / return connections. The pool can check after the connection returns that everything is all right.

, .

, . , . , , . , ( , , ). , , .

0

, : . , .

, , , ActiveRecord:: Base, , ( ), connection_connection, ,

, ActiveRecord, AR.

0

. , , . , , .

URL- Rails Apache Rewrite, . -. :

RewriteMap accounts prg:domain_mapper.rb
RewriteMap lowercase int:tolower

RewriteCond %{HTTP_HOST} ^(.*)$
RewriteCond ${accounts:${lowercase:%1}} ^(.+)$
RewriteRule . - [E=ACCOUNT:%1]
RequestHeader set Customer-Key %{ACCOUNT}e

, before_filter.

Rails . MySQL, , , MySQL.

I am sure that you have addressed migration issues. This was not a problem in the beginning with <2000. Now there are> 15 thousand Client databases (and growing), so we combine them back into a small number of plastered databases.

0
source

All Articles