From 44b64704dd542f114ac1cfab682a9ef30c384e82 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 13 Nov 2005 08:23:07 +0000 Subject: r3032@asus: jeremy | 2005-11-12 23:16:52 -0800 Ticket 428 - stale connections r3040@asus: jeremy | 2005-11-13 00:22:29 -0800 When AbstractAdapter#log rescues an exception, attempt to detect and reconnect to an inactive database connection. Connection adapter must respond to the active? and reconnect! instance methods. Initial support for PostgreSQL. References #428. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3000 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../connection_adapters/abstract_adapter.rb | 41 +++++++++++++--------- .../connection_adapters/postgresql_adapter.rb | 18 ++++++++++ 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 4c3938a64a..2543a2080d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* When AbstractAdapter#log rescues an exception, attempt to detect and reconnect to an inactive database connection. Connection adapter must respond to the active? and reconnect! instance methods. Initial support for PostgreSQL. [Jeremy Kemper] + * Much faster Oracle column reflection. #2848 [Michael Schoen ] * Base.reset_sequence_name analogous to reset_table_name (mostly useful for testing). Base.define_attr_method allows nil values. [Jeremy Kemper] diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 97f4bcde7f..7290f2e87e 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -47,25 +47,24 @@ module ActiveRecord protected def log(sql, name) - begin - if block_given? - if @logger and @logger.level <= Logger::INFO - result = nil - seconds = Benchmark.realtime { result = yield } - @runtime += seconds - log_info(sql, name, seconds) - result - else - yield - end + if block_given? + if @logger and @logger.level <= Logger::INFO + result = nil + seconds = Benchmark.realtime { result = yield } + @runtime += seconds + log_info(sql, name, seconds) + result else - log_info(sql, name, 0) - nil + yield end - rescue Exception => e - log_info("#{e.message}: #{sql}", name, 0) - raise ActiveRecord::StatementInvalid, "#{e.message}: #{sql}" + else + log_info(sql, name, 0) + nil end + rescue Exception => e + log_info("#{e.message}: #{sql}", name, 0) + reconnect_if_inactive! + raise ActiveRecord::StatementInvalid, "#{e.message}: #{sql}" end def log_info(sql, name, runtime) @@ -96,6 +95,16 @@ module ActiveRecord "%s %s" % [message, dump] end end + + private + def reconnect_if_inactive! + if respond_to?(:active?) and respond_to?(:reconnect!) + reconnect! unless active? + raise ActiveRecord::ConnectionFailed unless active? + else + @logger.warn "#{adapter_name} does not yet support automatic reconnection." if @logger + end + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 9c5b4c9c79..e1793ba460 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -52,6 +52,24 @@ module ActiveRecord 'PostgreSQL' end + # Is this connection alive and ready for queries? + def active? + # TODO: postgres-pr doesn't have PGconn#status. + if @connection.respond_to?(:status) + @connection.status != PGconn::CONNECTION_BAD + else + true + end + end + + # Close then reopen the connection. + def reconnect! + # TODO: postgres-pr doesn't have PGconn#reset. + if @connection.respond_to?(:reset) + @connection.reset + end + end + def native_database_types { :primary_key => "serial primary key", -- cgit v1.2.3