Help! Rails database connection guru needed!


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)
PGconn.connect(), logger )
5- connection_specification.establish_connection
@@defined_connections[self] = spec

The connection is made while Rails calls
PGconn.connect(), then the connection
stored 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

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

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: It is
to change the database, because the example give on
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 =>
:host => “”,
:database => “db1”,
:username => “user”,
:password => “pwd”)

# do something here !!!!!

# second connection to anther database
ActiveRecord::base.establish_connection(:adapter =>

:host => “”,
:database => “db2”,
:username => “user”,
:password => “pwd”)

# do something here agains !!!!!

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

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

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


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 amy help will be very


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs