Establish_connection does not raise exception for wrong pass

Hello,

When changing the default yaml-defined-connection via the
establish_connection in,
say, a login controller (we are using user-level access to our
database), wrong
credentials are not immediately notified (no exception is raised).
Instead, this
happens only at the first database access after the
establish_connection call:

ActiveRecord::Base.establish_connection(…)
rescue
… # not getting here

ActiveRecord::Base.establish_connection(…)
SomeTable.fetchData
rescue
… # getting here, $! containing (Oracle) database error message,
saying pwd is wrong

Is this a bug, or a feature?

Thanks for any hints,
Martin

Martin B. wrote:

Hello,

When changing the default yaml-defined-connection via the
establish_connection in,
say, a login controller (we are using user-level access to our

Thanks for any hints,
Martin

Establish connection have no exception saying that the pwd is wrong.

The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
may be returned on an error.

But you can use the connected? boolean method to know if you connected
to the database or not

Jamal S. wrote:

But you can use the connected? boolean method to know if you connected
to the database or not

Thanks for your reply. Your “solution” works, but the connected?
method does not return false in my case, it raises an exception.
Conceptually, I would prefer the boolean value…

Regards,
Martin

Martin wrote:

Jamal S. wrote:

But you can use the connected? boolean method to know if you connected
to the database or not

Thanks for your reply. Your “solution” works, but the connected?
method does not return false in my case, it raises an exception.
Conceptually, I would prefer the boolean value…

Regards,
Martin

What exception does it raise?


active?
Is this connection active and ready to perform queries?

Try it, maybe this doesn’t raise any exception.

Jamal S. wrote:

What exception does it raise?


active?
Is this connection active and ready to perform queries?

Try it, maybe this doesn’t raise any exception.

Now we are going details… actually, I already used

ActiveRecord::Base.connection.active?

I found out that this raises an OCIException, if called
after an establish_connection with wrong credentials.

ActiveRecord::Base.connected? always returns false for me,
even if the credentials are correct…

Btw, is there an easy way to find out what Exception was
raised when giving an empty rescue line?

Regards,
Martin

Martin wrote:

Jamal S. wrote:

What exception does it raise?
dentials are correct…

Btw, is there an easy way to find out what Exception was
raised when giving an empty rescue line?

What do you mean empty rescue line?

Regards,
Martin

Jamal S. wrote:

What do you mean empty rescue line?

OK, I had a look at the documentation now, the correct
description is

“write a rescue clause with no parameter list”

This defaults to StandardError. I would then like to find
out which subtype of StandardError was caught.

regards,
martin

Martin wrote:

Jamal S. wrote:

What do you mean empty rescue line?

OK, I had a look at the documentation now, the correct
description is

“write a rescue clause with no parameter list”

This defaults to StandardError. I would then like to find
out which subtype of StandardError was caught.

regards,
martin

If you write rescue without any parameter list you would catch all the
exception as you already know now…

I think you can still know which exception occurred by using one method,
cannot remember…

On Jun 2, 2007, at 3:34 PM, Jamal S. wrote:

This defaults to StandardError. I would then like to find
method,
cannot remember…

begin
raise NameError, “something went wrong”
rescue => e
puts “Caught: #{e.class.name}”
end

rescue => e
is exactly equivalent to:
rescue StandardError => e

If you need to catch more than StandardError, you can use:
rescue Exception => e

Or catch some other branch of the Exception hierarchy
rescue ScriptError => e # subclass of Exception
rescue LoadError => e # subclass of ScriptError

Or even less than StandardError
rescue IOError => e # subclass of StandardError
rescue EOFError => e # subclass of IOError

And, of course, ‘e’ is just a local variable so use your favorite
name if ‘e’ doesn’t feel right (but it is a very common idiom).

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

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