Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add connection reset and verification upon each connection checkout

  • Loading branch information...
commit a96b7d4c33757364a19ed1fc34f0a89801b8b2d7 1 parent d7d2d73
@nicksieger nicksieger authored
View
9 activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -261,12 +261,17 @@ def checkin(conn)
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
13 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -103,14 +103,23 @@ def disconnect!
@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
9 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -280,6 +280,15 @@ def disconnect!
@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 ======================================
Please sign in to comment.
Something went wrong with that request. Please try again.