Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 44b64704dd542f114ac1cfab682a9ef30c384e82 1 parent a762939
@jeremy jeremy authored
View
2  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]
View
41 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -47,25 +47,24 @@ def reset_runtime #:nodoc:
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 @@ def format_log_entry(message, dump = nil)
"%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
View
18 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -52,6 +52,24 @@ def adapter_name
'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",
Please sign in to comment.
Something went wrong with that request. Please try again.