1

我正在创建一个用户登录到 Oracle 数据库并运行准备好的查询的应用程序。我想使用 ActiveRecord 来帮助运行查询,但我遇到了一些问题。

当用户尝试登录时,我通过运行测试连接

def test (username, password)
ActiveRecord::Base.establish_connection({
    :adapter => 'oracle_enhanced',
    :database => '//database:1521/test',
    :username => username,
    :password => password
})
end

但是,如果用户输入了错误的登录信息,我会收到以下错误:

OCIError

ORA-01017: invalid username/password; logon denied

在导轨页面上。当我尝试重新加载登录页面时,它一直显示该错误并且不显示实际的登录页面。消除此错误的唯一方法是重新启动整个 Rails 应用程序。

这是因为我试图建立到另一个数据库的连接但它失败并且它不知道该怎么做?有什么办法可以防止这种情况发生吗?

4

1 回答 1

2

I also got this issue before. To test a db connection, you should not use ActiveRecord::Base because it effects on the entire app, but you should create an abstract class instead. Here is the code:

class DbConnectionTest < ActiveRecord::Base
  @abstract_class = true
end

Then you just simply use the custom class:

DbConnectionTest.establish_connection({})
于 2013-05-02T06:45:32.563 回答