mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Add connection reset and verification upon each connection checkout
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 ======================================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user