Add connection reset and verification upon each connection checkout

This commit is contained in:
Nick Sieger
2008-08-22 11:43:20 -05:00
parent d7d2d73d88
commit a96b7d4c33
3 changed files with 27 additions and 4 deletions

View File

@@ -261,12 +261,17 @@ module ActiveRecord
def checkout_new_connection
c = new_connection
@connections << c
@checked_out << c
c
checkout_and_verify(c)
end
def checkout_existing_connection
c = (@connections - @checked_out).first
checkout_and_verify(c)
end
def checkout_and_verify(c)
c.reset!
c.verify!(verification_timeout)
@checked_out << c
c
end

View File

@@ -103,14 +103,23 @@ module ActiveRecord
@active = false
end
# Reset the state of this connection, directing the DBMS to clear
# transactions and other connection-related server-side state. Usually a
# database-dependent operation; the default method simply executes a
# ROLLBACK and swallows any exceptions which is probably not enough to
# ensure the connection is clean.
def reset!
execute "ROLLBACK" rescue nil
end
# Returns true if its safe to reload the connection between requests for development mode.
# This is not the case for Ruby/MySQL and it's not necessary for any adapters except SQLite.
def requires_reloading?
false
end
# Lazily verify this connection, calling <tt>active?</tt> only if it hasn't
# been called for +timeout+ seconds.
# Lazily verify this connection, calling <tt>active?</tt> only if it
# hasn't been called for +timeout+ seconds.
def verify!(timeout)
now = Time.now.to_i
if (now - @last_verification) > timeout

View File

@@ -280,6 +280,15 @@ module ActiveRecord
@connection.close rescue nil
end
def reset!
if @connection.respond_to?(:change_user)
# See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
# reset the connection is to change the user to the same user.
@connection.change_user(@config[:username], @config[:password], @config[:database])
else
super
end
end
# DATABASE STATEMENTS ======================================