为什么在将Rails与Oracle一起使用时会出现“无监听器”错误?

时间:2010-02-01 11:08:58

标签: ruby-on-rails ruby oracle ora-12541

我使用Rails 2.3.5和Oracle 10作为我的数据库,使用“oracle_adapter”和ruby-oci8连接到Oracle主机。

我得到了这个例外:

Completed in 463ms (View: 18, DB: 166) | 200 OK [http://192.168.30.128/auctions?page=1]
/!\ FAILSAFE /!\  Mon Feb 01 19:02:11 +0800 2010
  Status: 500 Internal Server Error
  ORA-12541: TNS:no listener
    env.c:257:in oci8lib.so
    /home/qichunren/.gem/ruby/1.8/gems/ruby-oci8-1.0.7/lib/oci8.rb:229:in `initialize'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-oracle-adapter-1.0.0.9250/lib/active_record/connection_adapters/oracle_adapter.rb:623:in `new'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-oracle-adapter-1.0.0.9250/lib/active_record/connection_adapters/oracle_adapter.rb:623:in `new_connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-oracle-adapter-1.0.0.9250/lib/active_record/connection_adapters/oracle_adapter.rb:659:in `initialize'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-oracle-adapter-1.0.0.9250/lib/active_record/connection_adapters/oracle_adapter.rb:35:in `new'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-oracle-adapter-1.0.0.9250/lib/active_record/connection_adapters/oracle_adapter.rb:35:in `oracle_connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
    /usr/local/ruby187/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:183:in `checkout'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:98:in `connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:in `cache'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'
    /home/qichunren/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in `call'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/cgi_process.rb:44:in `dispatch_cgi'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:101:in `dispatch_cgi'
    /home/qichunren/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:27:in `dispatch'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:76:in `process'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `synchronize'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `process'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:159:in `process_client'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:158:in `each'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:158:in `process_client'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `initialize'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `new'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:285:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:268:in `initialize'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:268:in `new'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:268:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:282:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:281:in `each'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/configurator.rb:281:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/command.rb:212:in `run'
    /home/qichunren/.gem/ruby/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
    /home/qichunren/.gem/ruby/1.8/bin/mongrel_rails:19:in `load'
    /home/qichunren/.gem/ruby/1.8/bin/mongrel_rails:19

似乎与Oracle的连接经常断开连接。它显示oracle error:**ORA-12541: TNS:no listener**

我该如何解决这个问题?

oci8.c:270:in oci8lib.so: ORA-12541: TNS:no listener (OCIError)
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_oci_connection.rb:223:in `new'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_oci_connection.rb:223:in `new_connection'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_oci_connection.rb:328:in `initialize'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_oci_connection.rb:24:in `new'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_oci_connection.rb:24:in `initialize'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_connection.rb:9:in `new'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_connection.rb:9:in `create'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.2.4/lib/active_record/connec                                                                         tion_adapters/oracle_enhanced_adapter.rb:50:in `oracle_enhanced_connection'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/c                                                                         onnection_specification.rb:291:in `send'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/c                                                                         onnection_specification.rb:291:in `connection='
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/c                                                                         onnection_specification.rb:259:in `retrieve_connection'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/c                                                                         onnection_specification.rb:78:in `connection'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:2438:in `quoted_table_                                                                         name'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1259:in `find_one'
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1250:in `find_from_ids                                                                         '
        from /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:504:in `find'
        from script/maintenance/adjust_settlement.rb:19

4 个答案:

答案 0 :(得分:2)

首先你应该使用oracle_enhanced adapter,因为不再支持原始的oracle适配器。我还建议使用最新的ruby-oci8 version 2.0.3。并且还要检查您使用的是哪个Oracle客户端版本 - 我建议Oracle Instant Client 10.2.0.4或更高版本

但是如果你得到ORA-12541:TNS:没有监听器错误那么问题可能出现在与数据库的网络连接或Oracle服务器端的某些问题上。但请尝试oracle_enhanced适配器和最新的ruby-oci8版本,并告诉它是否能解决您的问题。

答案 1 :(得分:2)

确保:

  • 您在$ {ORACLE_HOME} /network/admin/tnsnames.ora中正确设置了连接名称。
  • 在加载oci8驱动程序之前设置ORACLE_HOME。

如果不起作用,请显式指定host [:port] / sid作为数据库名称。

E.g。如果你的主机是mydbhost.myorg.org,端口号是1522,sid是雇员,你可以使用它:

database: mydbhost.myorg.org:1522/employeedb

如果您的端口是默认端口1521,则可以省略冒号和端口号。

答案 2 :(得分:0)

我认为你的听众因没有活动而超时。我怀疑你正在打开连接,发出查询,然后在代码闲置等待另一个客户端请求时保持连接打开。如果您的数据库设置为允许连接是永久性的,但可以用尽所有池,这是大多数网站都不想要的。

您可以调整数据库以允许更长的会话,或者告诉它根本不断开连接,或者在尝试发送查询之前修改代码以检查连接,必要时重新连接,然后继续。

并非所有数据库适配器都允许您执行keep-alive / heartbeat甚至是询问服务器连接是否处于活动状态,因此我不得不恢复从异常中的表中包装一个简单的select 1处理程序,只是在继续之前戳连接。

另一种选择是将一个简单的“保持活动”例程放在一个单独的线程中,该线程定期执行一个简单的请求,如select 1,以保持数据库满意。如果该例程检测到连接已断开,那么它将负责将“db_connection_up”标志设置为false或重新连接。

您必须根据您要处理的请求数量确定处理它的最佳方式。连接,发出一个微小的请求,然后重复断开,这是一个不同的坏事,所以你必须找到一个愉快的媒介。

与您的DBA联系,了解Oracle DB中的超时,以及是否可以更改或者是否可以获得永久连接。如果没有,请调整您的代码。

在google搜索“oracle listener timeout”以获取更多信息。

答案 3 :(得分:0)

凯文的回答对我有用。另外,还有另一种方法。

我使用的是rails 4.1.x,Gemfile:

gem 'activerecord-oracle_enhanced-adapter', '~> 1.5.0'
gem 'ruby-oci8', '2.2.3'

database.yml:

# not work!
host:  218.5.173.xxx/my_db

# works, solution 1:  
database: 218.5.173.xxx/my_db


# works, solution 2: 
database: "(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=218.5.173.xxx)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=my_db))
)"