Can I use 2 DB connections in my app?

Sure, try this on a model object (say called Planet):

class Planet < ActiveRecord::Base
  establish_connection(:adapter => "mysql", :host => "mydbserver.com",
:database => "myDb" .... )

end

where '...' are the other paramters to use in the connection. This
will result in the Planet model using the db connection defined above.

Dmitry Hazin wrote:

5MileRadius wrote:
> Sure, try this on a model object (say called Planet):
>
> class Planet < ActiveRecord::Base
> establish_connection(:adapter => "mysql", :host => "mydbserver.com",
> :database => "myDb" .... )
>
> end
>
> where '...' are the other paramters to use in the connection. This
> will result in the Planet model using the db connection defined above.

But how can I differentiate between a reader and a writer database? It
seems to be rails limitation here..

Something along the lines of pgpool
<http://pgpool.projects.postgresql.org/> could be appropriate for a
master/slave setup.

Isak

these might be of help as well:
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc
http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases

ed

[...]
} Something along the lines of pgpool
} <http://pgpool.projects.postgresql.org/> could be appropriate for a
} master/slave setup.

I like PostgreSQL and would like to use it, but I have to do my due
diligence in looking for similar solutions on top of our current RDBMS
(MySQL) and, potentially, any commercial databases.

The pgpool solution (or, rather, pgpool-II) looks great in that both
read/write partitioning and vertical (row) partitioning is completely
transparent to the application layer. Does anyone know of anything like
that, commercial or otherwise, exist for MySQL or the various commercial
RDBMSs?

} Isak
--Greg

Is there a way to maintain two open connections simultaneously using
ActiveRecord::Base?

for example, in a generic script with 'dbi' or something, I could
establish two connections as such:

class Something
      def test
            dbh1 = DBI.connect("dbi:Mysql:test:system1", "user", "pass")
            dbh2 = DBI.connect("dbi:Oracle:test:system2", "user", "pass")
            accounts = {
                   "system1" => dbh1.distinct("users from users"),
                   "system2" => dbh2.distinct("usernames from user_acc")
             }
            dbh1.disconnect
            dbh2.disconnect
            return accounts
      end
end

This is bs code but, can two activerecord::base connections be
established simultaneously like this?

There are no issues at all with establishing as many connections as you
wish. This issue lies more with linking them to a model more then
making the connection.

What is below is an example - stolen blatantly from earlier in the thread…

class Planet < ActiveRecord::Base

establish_connection(:adapter => “mysql”, :host => “mydbserver.com”,

:database => “myDb” … )

end

To do what you want below - I might be tempted to have a base model for
users and then create at least one subclass that had look like

class DBxUser < User

establish_connection…(to DBx)

end

Then from your controller create both a User and a DBxUser and then
make you concatenated array by calling into both of them. I’m assuming
that at least one of the two database connections is the standard
connection you use for the rest of your connection (i.e. the one listed
under config/database.yml).

John W Higgins

wishdev@gmail.com

this has been discussed numerous times on the list and is documented here as well:

http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabases

http://wiki.rubyonrails.org/rails/pages/HowtoSwitchBetweenMultipleDevelopmentDatabases

http://wiki.rubyonrails.org/rails/pages/HowtoUseMultipleDatabasesOnePerTable

ed