Helps! Rails database connection guru needed!

Hi,

I really need help to understand what is happening
while Rails connect to a database, (we area actually
using postgresql, so my example will be with
postgresql) What I understand is Rails is able to
connect to different DB. Actually, this is what we
want, and we have made some tests, everything is juste
fine. However, all these tests lead us to some
important questions that we did find the answer from
the documentation, So we hope the communauty can help
us to answer those questions!!!

Question A- How rails disconnect the connection?
As we can see in the source code :

Call stack for postgresql

1- any controller
2- connectionspecification.establish_connection
3- ActiveRecord::base.postgresql_connection(config)
4- ConnectionAdapters::PostgreSQLAdapter.new(
PGconn.connect(), logger )
5- connection_specification.establish_connection
@@defined_connections[self] = spec

The connection is made while Rails calls
PGconn.connect(), then the connectionstored in
@connection of the adapter class. while a request is
send to the adapter,the adapter will sends the request
to the database via its variable @connection.But where
exactly the disconnection is done while we connect
ourself to another database??? As we can see in the
source code “remove_connection”

connectionspecification
def self.remove_connection(klass=self)
conn = @@defined_connections[klass]
@@defined_connections.delete(klass)
active_connections[klass] = nil
conn.config if conn
end

bascially, it removes the “table-linked-class” from
the class variable @@defined_connection and
@@active_connections. Actually it does not do anything
to disconnect the actually connection, So we wonder
will it lead to a memory leak of the ruby-rails and
the connected database??? Is there anything we miss
that make us misunderstand the mecanism of connection
and reconnection???

Question B
inside the same methode of a controller, like:
www.myrails.com/mycontroller/mymethode1. It is
possible
to change the database, because the example give on
the
Peak Obsession
the basic idea is to change the connection, while user
change methode(link to a new page or something…) is
it possible to change the database inside the same
method? like

class mycontroller < ApplicationController

def mymethode1
# first connection
ActiveRecord::base.establish_connection(:adapter =>
“postgresql”,:host => “1.2.3.4”, :database => “db1”,
:username => “user”, :password => “pwd”)

# do something here !!!!!

# second connection to anther database

ActiveRecord::base.establish_connection(:adapter =>
“postgresql”, :host => “1.2.3.5”,:database => “db2”,
:username => “user”, :password => “pwd”)

# do something here agains !!!!!

# third connection to another database
ActiveRecord::base.establish_connection(:adapter =>

“postgresql”,:host => “1.2.3.6”,:database => “db3”,
:username => “user”, :password => “pwd”)

# do something here agains !!!!!
    # then done

end

Can we do this??? If we have connection to three
different databases, what Rails don exactly??? Does it
have three connections inside somewhere or It
disconnect the old one each time we make a new
connection??? or there is other mecanism??? And how
does it manange all this connection???

Thanks you very much and any help will be very
appreciated!!!

Saiho


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

Hi,

I really need help to understand what is happening
while Rails connect to a database, (we area actually
using postgresql, so my example will be with
postgresql) What I understand is Rails is able to
connect to different DB. Actually, this is what we
want, and we have made some tests, everything is juste
fine. However, all these tests lead us to some
important questions that we did find the answer from
the documentation, So we hope the communauty can help
us to answer those questions!!!

Question A- How rails disconnect the connection?
As we can see in the source code :

Call stack for postgresql

1- any controller
2- connectionspecification.establish_connection
3- ActiveRecord::base.postgresql_connection(config)
4- ConnectionAdapters::PostgreSQLAdapter.new(
PGconn.connect(), logger )
5- connection_specification.establish_connection
@@defined_connections[self] = spec

The connection is made while Rails calls
PGconn.connect(), then the connectionstored in
@connection of the adapter class. while a request is
send to the adapter,the adapter will sends the request
to the database via its variable @connection.But where
exactly the disconnection is done while we connect
ourself to another database??? As we can see in the
source code “remove_connection”

connectionspecification
def self.remove_connection(klass=self)
conn = @@defined_connections[klass]
@@defined_connections.delete(klass)
active_connections[klass] = nil
conn.config if conn
end

bascially, it removes the “table-linked-class” from
the class variable @@defined_connection and
@@active_connections. Actually it does not do anything
to disconnect the actually connection, So we wonder
will it lead to a memory leak of the ruby-rails and
the connected database??? Is there anything we miss
that make us misunderstand the mecanism of connection
and reconnection???

Question B
inside the same methode of a controller, like:
www.myrails.com/mycontroller/mymethode1. It is
possible
to change the database, because the example give on
the
Peak Obsession
the basic idea is to change the connection, while user
change methode(link to a new page or something…) is
it possible to change the database inside the same
method? like

class mycontroller < ApplicationController

def mymethode1
# first connection
ActiveRecord::base.establish_connection(:adapter =>
“postgresql”,:host => “1.2.3.4”, :database => “db1”,
:username => “user”, :password => “pwd”)

# do something here !!!!!

# second connection to anther database

ActiveRecord::base.establish_connection(:adapter =>
“postgresql”, :host => “1.2.3.5”,:database => “db2”,
:username => “user”, :password => “pwd”)

# do something here agains !!!!!

# third connection to another database
ActiveRecord::base.establish_connection(:adapter =>

“postgresql”,:host => “1.2.3.6”,:database => “db3”,
:username => “user”, :password => “pwd”)

# do something here agains !!!!!
    # then done

end

Can we do this??? If we have connection to three
different databases, what Rails don exactly??? Does it
have three connections inside somewhere or It
disconnect the old one each time we make a new
connection??? or there is other mecanism??? And how
does it manange all this connection???

Thanks you very much and any help will be very
appreciated!!!

Saiho


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around