1

I need to establish connection to multitple databases at the same time using the gem 'active_record'

i am using below code to achieve that:

require 'active_record'

conn1 = ActiveRecord::Base.establish_connection(
        :adapter => 'oracle_enhanced',
        :database => "//db1",
        :username => 'user1',
        :password => 'pwd1'
    )
a = conn1.connection.exec_query("select N_WF_NIE from COMBINED")
puts a

conn2 = ActiveRecord::Base.establish_connection(
        :adapter => 'oracle_enhanced',
        :database => "db2",
        :username => 'user2',
        :password => 'pwd2'
    )
b = conn2.connection.exec_query("select MSLNN from PICAAMISC")
puts b

Now further in the script when I use the first connection(conn1) that is nullified and the below query does not return any result.

c = conn1.connection.exec_query("select N_NIE from AGG")
puts c

I want to make sure that both my connections are active at the same time so that I can use any of the connection in the script if required, is there any way to achieve that?

s c
  • 71
  • 1
  • 11
  • Why do you want to achieve this ? Do you want to create a Multitenancy app ? Don't you have any better options ? – XavM Aug 30 '16 at 07:37
  • @XavM, suppose I have 2 databases and I want to compare the schema of both ,for example tables, columns, data etc. and that too one by one. so for all the cases i establish connection to one db then another and then compare both, then again for other test i again need to establish connection. i wanted to create connection one time only and then use the same in the script – s c Aug 30 '16 at 07:42
  • Possible duplicate of [Connecting Rails 3.1 with Multiple Databases](http://stackoverflow.com/questions/6122508/connecting-rails-3-1-with-multiple-databases) – Nick Roz Aug 30 '16 at 08:13
  • Have a look at http://stackoverflow.com/questions/6122508/connecting-rails-3-1-with-multiple-databases and http://stackoverflow.com/questions/17311199/connecting-to-multiple-databases-in-ruby-on-rails – Nick Roz Aug 30 '16 at 08:13
  • @Nick Roz:He already uses establish_connection method, like in these posts. '@s c: I'm not really sure you can have simultaneous connection with two different db. You could store schema of one db and then connect to the second one to compare each other – XavM Aug 30 '16 at 08:51
  • @NickRoz, No above Links doesn't help in my case, Please let me know if there is any other way out – s c Aug 30 '16 at 09:38
  • Try https://github.com/thiagopradi/octopus – Nick Roz Sep 06 '16 at 17:43

0 Answers0