Initialize database connection when my rails project start

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

On Feb 11, 9:03 am, Valentino L. [email protected]
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

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