establish_connection does not raise exception for wrong password

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(...<wrong pwd>)   rescue     ... # not getting here

  ActiveRecord::Base.establish_connection(...<wrong pwd>)   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

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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com