aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorNick Sieger <nick@nicksieger.com>2008-08-22 11:43:20 -0500
committerNick Sieger <nick@nicksieger.com>2008-08-29 14:12:12 -0500
commita96b7d4c33757364a19ed1fc34f0a89801b8b2d7 (patch)
treecaf325b1a09493e6ca4c150b762484020a5688ea /activerecord/lib/active_record
parentd7d2d73d88e465cb0c618283862179accd8932e1 (diff)
downloadrails-a96b7d4c33757364a19ed1fc34f0a89801b8b2d7.tar.gz
rails-a96b7d4c33757364a19ed1fc34f0a89801b8b2d7.tar.bz2
rails-a96b7d4c33757364a19ed1fc34f0a89801b8b2d7.zip
Add connection reset and verification upon each connection checkout
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb9
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb13
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb9
3 files changed, 27 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
index 988f85e909..365c80fe1d 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -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
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index 4b78d9f2e9..c37ebf1410 100755
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -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
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 204ebaa2e2..0a935a1b7a 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -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 ======================================