How to disconnect to a database?


#1

Hi,

We have experienced some problems with
ActiveRecord::Base class of Ruby-Rails. We are
building a web-application based on the Ruby-rails
framework and the web-application needs to access to
difference databases, so we do not pre-define our
database accesses in the database.yml file. In fact,
we are using the
ActiveRecord::Base.establish_connection() to connect
to our database, the function works perfectly.
However, while we try to disconnect our database
connection with
ActiveRecord::Base.remove_connection(), we find out
that the connection is not closed (the function seems
not to be working perfectly) and we are still
connected to the database and able to access to the
tables.

Can you please tell us, is there something we did
wrong? or it is a known problem?
and what should we do to disconnect a database
connection ?

The using version ruby-rails are :
Rails 0.14.2 and ruby 1.8.3 (2005-09-21)
[x86_64-linux]
We are running on Linux KDE 3.4, and our database
engine is postgresql

The following is a code example:
####################################################################################

class GenTableAs < ActiveRecord::Base
end

class GentestController < ApplicationController

def index
	GenTableAs.establish_connection(:adapter =>

“postgresql”,
:host => “1.2.3.4”,
:port => 5432,
:database => “gendb”,
:username => “dbuser”,
:password => “dbuser”)
@connected1 = GenTableAs.connection()
@removedConnection = GenTableAs.remove_connection()

	@connected2 = GenTableAs.connection() # should be

nil here
obj1 = GenTableAs.new
obj1[“colaa”] = 100
obj1[“colab”] = “100”
obj1.save # should have raised exception or error
here
end
end
####################################################################################
the output of index

@connected1:

#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x2aaaac138930
@runtime=0.0189187526702881,
@connection=#PGconn:0x2aaaac138980,
@logger=#<Logger:0x2aaaac2e7fd8
@default_formatter=#<Logger::Formatter:0x2aaaac2e7dd0
@datetime_format=nil>, @progname=nil,
@logdev=#<Logger::LogDevice:0x2aaaac2e7ce0
@shift_size=1048576, @shift_age=0,
@filename="./log/development.log",
@mutex=#<Logger::LogDevice::LogDeviceMutex:0x2aaaac2e7c90
@mon_waiting_queue=[], @mon_entering_queue=[],
@mon_count=0, @mon_owner=nil>,
@dev=#<File:./log/development.log>>, @level=0,
@formatter=nil>>

removed connection:

:password: dbuser
:database: gendb
:host: 1.2.3.4
:port: 5432
:adapter: postgresql
:username: dbuser

@connected2:

#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x2aaaac138930
@runtime=0.0189187526702881,
@connection=#PGconn:0x2aaaac138980,
@logger=#<Logger:0x2aaaac2e7fd8
@default_formatter=#<Logger::Formatter:0x2aaaac2e7dd0
@datetime_format=nil>, @progname=nil,
@logdev=#<Logger::LogDevice:0x2aaaac2e7ce0
@shift_size=1048576, @shift_age=0,
@filename="./log/development.log",
@mutex=#<Logger::LogDevice::LogDeviceMutex:0x2aaaac2e7c90
@mon_waiting_queue=[], @mon_entering_queue=[],
@mon_count=0, @mon_owner=nil>,
@dev=#<File:./log/development.log>>, @level=0,
@formatter=nil>>

Thanks you very much !!!

Saiho


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com


#2

The following link will point you to how to manage multiple databases.

http://wiki.rubyonrails.com/rails/pages/HowtoUseMultipleDatabases

Basically, you use a before_filter to reset the connection on each
request
(no need to disconnect).

I’m still concerned about whether or not a race condition would exist if
two
users simultaneously accessed Ruby. Would their connections on different
db’s interfere?

Without using the before_filter, they would. With it, I’m hoping the
connections are not shared between requests. If not, then the technique
shown is of no use.

Can someone who knows this well comment? This is the second time this
issue
has appeared in two days, and I think this is something which certain
types
of applications must do.

James