Valentino Lun
2009-Feb-11  09:03 UTC
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
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-11  09:29 UTC
Re: Initialize database connection when my rails project start
On Feb 11, 9:03 am, Valentino Lun <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> > 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Valentino Lun
2009-Feb-11  10:05 UTC
Re: Initialize database connection when my rails project start
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---