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?

7
  • Why do you want to achieve this ? Do you want to create a Multitenancy app ? Don't you have any better options ? Commented Aug 30, 2016 at 7: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 Commented Aug 30, 2016 at 7:42
  • Possible duplicate of Connecting Rails 3.1 with Multiple Databases Commented Aug 30, 2016 at 8:13
  • Have a look at stackoverflow.com/questions/6122508/… and stackoverflow.com/questions/17311199/… Commented Aug 30, 2016 at 8: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 Commented Aug 30, 2016 at 8:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.