diff options
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 <schoenm@earthlink.net>] * 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", |