aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-11-13 08:23:07 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-11-13 08:23:07 +0000
commit44b64704dd542f114ac1cfab682a9ef30c384e82 (patch)
treef17038b337f0c443963b4f7215ac20aaea3ef728 /activerecord
parenta762939894ec2f825ccafd439ce17bb30fc65dc3 (diff)
downloadrails-44b64704dd542f114ac1cfab682a9ef30c384e82.tar.gz
rails-44b64704dd542f114ac1cfab682a9ef30c384e82.tar.bz2
rails-44b64704dd542f114ac1cfab682a9ef30c384e82.zip
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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb41
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb18
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",