Forum: Ruby on Rails Initialize database connection when my rails project start

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af3cecc8af253b5acd3c09c5b67c0074?d=identicon&s=25 Valentino Lun (on9west)
on 2009-02-11 10:03
Dear all

I have the following ruby code that can establish multiple(around 300)
database connection and stored them in a hash (conn_pool).

I can use conn_pool["Keys"].connection.execute("select * from table") to
get the SQL result.

My question is that, how can I initialize these database connections
when my rails project started? So I can access conn_pool[something] in
controllers.

Please give me some advices.

Thank you very much

Valentino

require 'rubygems'
require 'resolv'
require 'activerecord'

class LIS_SSC_ST1 < ActiveRecord::Base
end

LIS_SSC_ST1.establish_connection(
:host => "localhost",
:adapter => "jdbc",
:dialect => "sybase",
:autocommit => false,
:driver => "com.sybase.jdbc3.jdbc.SybDataSource",
:url => "jdbc:sybase:Tds:cdcibm74.server.ha.org.hk:22601/SSC_DB",
:username => "username",
:password => "password")

class Server < LIS_SSC_ST1
    set_table_name "ssc_servers"
end

r = Regexp.new("LIS_..._SP(1[012])")
conn_pool = {}

def is_active?(dns,host)
  Resolv.getaddress(dns) == Resolv.getaddress(host) ? true : false
end

def make_conn(h,server_name)
    eval("class #{server_name} < ActiveRecord::Base
          end")
    eval(server_name).establish_connection(h)
end

Server.find(:all, :conditions => "server_type = 'sybase'", :order =>
"server_name").each do |x|
  if x.server_name =~ r
    server_name = x.server_name.gsub("SP","ST")
    server_dns = x.server_name.gsub("_SB","").gsub("_","-").downcase
    if is_active?(server_dns,x.server_host)
        h = {:host => "localhost",
             :adapter => "jdbc",
             :dialect => "sybase",
             :autocommit => false,
             :driver => "com.sybase.jdbc3.jdbc.SybDataSource",
             :url =>
"jdbc:sybase:Tds:#{server_dns}:#{x.port.to_s.gsub(/^4/,"2")}/LAB_DB",
             :username => "username",
             :password => "password"}

        conn_pool[server_name] = make_conn(h,server_name)
    end
  end
end
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-11 10:30
(Received via mailing list)
On Feb 11, 9:03 am, Valentino Lun <rails-mailing-l...@andreas-s.net>
wrote:
> Dear all
>
> I have the following ruby code that can establish multiple(around 300)
> database connection and stored them in a hash (conn_pool).
>
> I can use conn_pool["Keys"].connection.execute("select * from table") to
> get the SQL result.

Why not just do "Keys".constantize (will will return the Keys class) ,
ie

foo.constantize.connection.execute 'bla bla bla" since it seems that
the objects in your hash are just the corresponding ActiveRecord
classes ?

Failing that I;d have the conn_pool thingy be an instance variable of
the Server class with an accessor (so you could do Server.conn_pool
[...]). This all looks a little strange though.

Fred
Af3cecc8af253b5acd3c09c5b67c0074?d=identicon&s=25 Valentino Lun (on9west)
on 2009-02-11 11:05

hi Fred,

Thank you for your reply.

I just load the script in config/initializers and make the conn_pool to
@conn_pool. It work fine.

But I want to learn more.
Could you further elaborate on this, how can I do this in Server Class??
I have really no ideas..(I am a not really experienced in rails)

> Failing that I;d have the conn_pool thingy be an instance variable of
> the Server class with an accessor (so you could do Server.conn_pool
> [...]). This all looks a little strange though.

Thanks again

Valentino
This topic is locked and can not be replied to.