mirror of
https://github.com/github/rails.git
synced 2026-04-04 03:00:58 -04:00
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
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user