Unless connected? + multiple databases

Hello all.

I am still having fun with multiple databases. In this case it is the
unless connected? statement.

ActiveRecord::Base establishes a connection that is inherited by the
majority of my models. However one Model (Componentlog) that uses a
separate DB is coded as:

unless Componentlog.connected?
establish_connection(
:adapter => “oci”,
:database => “”,
:host => “oracle7”,
:username => “vcgeneral”,
:password => “manhattan”
)
end

I use unless connected? to stop it using a new connection every time the
model is acccessed.

This works fine as long as that model is the first accessed by the
application. Because ActiveRecord::Base hasn’t established it’s
connection yet (From the looks of it) this Model happily establishes its
own connection.

If, however, ActiveRecord::Base HAS established a connection this class
will use that (incorrect) one because I assume it inherits from
ActiveRecord::Base and the connected? method checks all superclasses

What I need to say essentially is that When it comes to the connection
this class doesn’t inherit the connections.

Does anyone know a way to do this?

I have tried having an intermediate class that this one inherits from
where the establish connection is over-ridden but if connected? doesn’t
see a connection there is still tracks up to ActiveRecord::Base instead
of saying “Ok, it isn’t in this class, I’ll establish a new connection”

This is all what I think is happening. I am a beginner so if I am
missing something obvious I do apologise :slight_smile:

Now that I think about it. I will try removing the unless connected? bit
because I am now running mongrel with allow_concurrency off in
environment.rb.

Jeff

I have tried having an intermediate class that this one inherits from
where the establish connection is over-ridden but if connected? doesn’t
see a connection there is still tracks up to ActiveRecord::Base instead
of saying “Ok, it isn’t in this class, I’ll establish a new connection”

Having looked at the connected? method it is plain that it uses the
active_connections method to check the connection for a given class. So
by calling active_connections directly using
ActiveRecord::Base.active_connections[self.name]? it will only check for
this class and not the super classes.

I have always thought that the built in RoR modules etc would be
stupidly complex and beyond my ken (having done c++ at uni and
experiencing that feeling) but I am glad that this understandable
Framework is different :slight_smile:

Jeff

Hi,

I’m using also two db with rails, basically what I did
is create two sub-class (correspond to the two db)
from activeRecord::base. And I never use
etablish_connection on the avtiveRecord::bas, but only
connect to the db via the two sub classes, all the
tables are sub-classes of the two main sub (depend on
which data base it should be connected) When I know I
have to swtich database, I only have to tell rails by
using etablish_connection on the main sub-classes. It
works perfectly for me. I never have problem with the
connection.

Rails keeps the connections inside an internal hash.
when a connection is asked for the first time, raisl
will connect to the db and keep this connection, So
there are always as many db as conneciton open, and It
gives the right one when it is necessary (if you ask).
So you don’t have “remove” a connection… rails do
it for you.

hope it helps

Saiho

— Jeff J. [email protected] wrote:

(Componentlog) that uses a
end
happily establishes its
to the connection
of saying "Ok, it isn’t in this class, I’ll
environment.rb.
http://lists.rubyonrails.org/mailman/listinfo/rails

The mind is its own place, and in itself.
Can make a Heaven of Hell, a Hell of Heaven.


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

As an aside, there seems to be a ActiveRecord inconsistency in that
remove_connection() doesnâ??t actually close down the connection, so
instead of a

X.establish_connection()
...
X.remove_connection()

pattern, where X is the table, try using something like

unless X.connected? X.establish_connection()
...
X.remove_connection() # for good form

I noticed this while having my own ActiveRecord derived classes
referencing different tables in the same db.

Saiho sayoyo wrote:

Hi,

I’m using also two db with rails, basically what I did
is create two sub-class (correspond to the two db)
from activeRecord::base. And I never use
etablish_connection on the avtiveRecord::bas, but only
connect to the db via the two sub classes, all the
tables are sub-classes of the two main sub (depend on
which data base it should be connected) When I know I
have to swtich database, I only have to tell rails by
using etablish_connection on the main sub-classes. It
works perfectly for me. I never have problem with the
connection.

This approach didn’t work for me because ActiveRecord::Base
always established a connection based on the database.yaml.
If I didn’t include anything in the YAML file then the webrick
would error out on startup. How did you stop a Base connection
automatically
being created on startup?

The method I have used appears to be working but it isn’t pretty.