# Helps! Rails database connection guru needed!

**URL:** <https://www.ruby-forum.com/t/helps-rails-database-connection-guru-needed/52752>\
**Category:** Rails\
**Created:** [January 18, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/helps-rails-database-connection-guru-needed/52752 "2006-01-18T18:06:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saiho\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b38774/32.png) [@Saiho\_Y](https://www.ruby-forum.com/u/Saiho_Y)\
**Post date:** [January 18, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/helps-rails-database-connection-guru-needed/52752/1 "2006-01-18T18:06:32Z")

</div>

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](http://www.myrails.com/mycontroller/mymethode1). It is  
possible  
to change the database, because the example give on  
the  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases)  
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

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![Saiho\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b38774/32.png) [@Saiho\_Y](https://www.ruby-forum.com/u/Saiho_Y)\
**Post date:** [January 19, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/helps-rails-database-connection-guru-needed/52752/2 "2006-01-19T16:53:54Z")

</div>

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](http://www.myrails.com/mycontroller/mymethode1). It is  
possible  
to change the database, because the example give on  
the  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases)  
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

> **[\[object](https://mail.yahoo.com/)**
>
> \[object
