ruby – ActiveRecord:当连接断开时如何自动重新连接到PostgreS
发布时间:2020-12-17 02:07:10 所属栏目:百科 来源:网络整理
导读:我正在使用ActiveRecord与Sinatra和PostgreSQL.当数据库连接中断(由于临时网络故障或postgres服务器重新启动)时,我的应用程序不会自动重新获取连接.我将不得不重新启动应用程序才能再次连接到postgres.我记得当我在另一个项目中使用Rails时,我没有遇到这个问
我正在使用ActiveRecord与Sinatra和PostgreSQL.当数据库连接中断(由于临时网络故障或postgres服务器重新启动)时,我的应用程序不会自动重新获取连接.我将不得不重新启动应用程序才能再次连接到postgres.我记得当我在另一个项目中使用Rails时,我没有遇到这个问题.
我是否需要放置一些配置或代码来告诉ActiveRecord自动重新连接到PostgreSQL? 解决方法
ActiveRecord的:: Base.verify_active_connections!已经在2012年的rails commit 9d1f1b1ea9e5d637984fda4f276db77ffd1dbdcb中删除了.所以我们不能使用那种方法.
以下句子是我的简短调查结果.我不是rails activerecord的专家.所以要谨慎听. (但希望这有帮助) comment in connection_pool.rb说 # 1. Simply use ActiveRecord::Base.connection as with Active Record 2.1 and # earlier (pre-connection-pooling). Eventually,when you're done with # the connection(s) and wish it to be returned to the pool,you call # ActiveRecord::Base.clear_active_connections!. This will be the # default behavior for Active Record when used in conjunction with # Action Pack's request handling cycle. 所以也许你(和我有同样的情况就像你一样)必须返回池连接. 并作为Action Pack的请求处理周期返回sinatra中池的连接,使用ActiveRecord :: ConnectionAdapters :: ConnectionManagement use ActiveRecord::ConnectionAdapters::ConnectionManagement 然后如rails commit 9d1f1b1ea9e5d637984fda4f276db77ffd1dbdcb中所述,我们使用与this line中不同的方式,当通过遵守动作包生命周期使用Basae.connection时始终checkout_and_verify. def connection # this is correctly done double-checked locking # (ThreadSafe::Cache's lookups have volatile semantics) @reserved_connections[current_connection_id] || synchronize do @reserved_connections[current_connection_id] ||= checkout end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |