Saiho Yuen
2006-Jan-18 16:29 UTC
[Rails] 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 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 http://mail.yahoo.com
Saiho Yuen
2006-Jan-19 15:15 UTC
[Rails] Helps!!!!! Rails database connection guru needed!!!!!!! 2nd Try!!!!!
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 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 http://mail.yahoo.com
Maybe Matching Threads
- establish_connection method
- [PATCH] Support for DB Clusters/Replication in ActiveRecord (RFC)
- If you want to disconnect a database properly, there is the code:)!!!
- HOWTO: Combine fields from 2 two tables in 1 object
- Rails not properly handling Oracle db connections/sesions in dev mode