2 Replies Latest reply: May 7, 2008 11:10 PM by 575020 RSS

    Concurrent Connections to Multiple Oracle Databases in RoR


      In Obie Fernadez' article in http://www.oracle.com/technology/pub/articles/fernandez-rails-connections.html showing how to connect to multiple Oracle databases, a part of the article says:

      "Using More Than One Database

      Setting up a Rails application to talk to more than one database is simple. Just specify the extra database connection parameters in the database.yml file. For example, suppose you needed to access a different Oracle database for inventory data:

      adapter: oci
      host: xe
      username: inventory
      password: password     

      Then define a Model (called Inventory) used to access inventory data:

      class Forum < ActiveRecord::Base
      self.connection = "inventory"

      Rails applications are deployed as pools of independent server processes, and one database connection is maintained and reused per process for every rails request. Connections are usually created through ActiveRecord::Base.establish_connection and retrieved by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this connection. But you can also set a class-specific connection for specific Models (as shown in the previous section) after which it and all its subclasses will use the explicitly-specified connection instead.

      ActiveRecord keeps a connection pool in ActiveRecord::Base as a Hash indexed by the name of the model class. When a connection is requested, the retrieve_connection method will go up the class-hierarchy until a connection is found in the connection pool.

      In here he says define a model called "Inventory" and yet what is being defined is a model called "class Forum < ActiveRecord::Base".

      Question1 : Are there ways of doing concurrent multiple connections for Oracle DBs without using Dr. NIC's MMC and a practical quick guide or database.yml configured for Oracle?

      Example in database.yml for Instance/SID - oplnx921, Schema/Username - devapp01
      # remote Oracle 10g
      development:<---Question 2: what do we place in here?
      adapter: oracle
      database: linx116.abcde.mycompany.com:1521/oplnx921
      username: devapp01
      password: xxxxxxxx

      Question 3: How is the following different instance/tnsname coded in the database.yml which I want to connect to in the same RoR application?

      Instance/SID - oplnx920
      Schema/Username - tstapp01
      Password: xxxxxxxx

        • 1. Re: Concurrent Connections to Multiple Oracle Databases in RoR

          In Oracle on Rails, it also uses ActiveRecord to connect to the Database, so that means models are allowed one connection to a database at a time, per class. In Obie Fernadez's article on OTN, here "Using More Than One Database" means you can create different models with different database connections, but you can have only one connection for each model.

          Suppose we have following connections and models:
          #in database.yml

          adapter: oci
          host: xe
          username: inventory1
          password: password

          adapter: oci
          host: xe
          username: inventory2
          password: password

          And we can defined two models with different connection enabled:

          #in your model

          class Forum1 < ActiveRecord::Base
          self.connection = "inventory1"

          class Forum2 < ActiveRecord::Base
          self.connection = "inventory2"

          Here in the example, we are "Using More Than One Database", but notice each model has only one connection. It is no possibility if you want to use a concurrent connections of "invetory1" and "invetory2" for Model "Forum1".

          The native Rails doesn't provide such Concurrent Connections functionality, if you do want to use this feature, you have to use Dr. NIC's MMC.
          • 2. Re: Concurrent Connections to Multiple Oracle Databases in RoR
            I'd like to add one point for Question 2 "development:<---Question 2: what do we place in here?".
            When we use the flags like inventory1:, inventory2:, they are not development: nor test: nor production:. So some default behaviors of development:, test: or production: (e.g., production: enables Cache by default) will not apply to inventory1 and inventory2. Fortunately we can tune the classes Forum1 and Forum2, they are flexible to be defined to meet our particular requirements.

            Message was edited by: