Skip to content
This repository
Browse code

Add connection reset and verification upon each connection checkout

  • Loading branch information...
commit a96b7d4c33757364a19ed1fc34f0a89801b8b2d7 1 parent d7d2d73
Nick Sieger authored August 22, 2008
9  activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -261,12 +261,17 @@ def checkin(conn)
261 261
       def checkout_new_connection
262 262
         c = new_connection
263 263
         @connections << c
264  
-        @checked_out << c
265  
-        c
  264
+        checkout_and_verify(c)
266 265
       end
267 266
 
268 267
       def checkout_existing_connection
269 268
         c = (@connections - @checked_out).first
  269
+        checkout_and_verify(c)
  270
+      end
  271
+
  272
+      def checkout_and_verify(c)
  273
+        c.reset!
  274
+        c.verify!(verification_timeout)
270 275
         @checked_out << c
271 276
         c
272 277
       end
13  activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -103,14 +103,23 @@ def disconnect!
103 103
         @active = false
104 104
       end
105 105
 
  106
+      # Reset the state of this connection, directing the DBMS to clear
  107
+      # transactions and other connection-related server-side state. Usually a
  108
+      # database-dependent operation; the default method simply executes a
  109
+      # ROLLBACK and swallows any exceptions which is probably not enough to
  110
+      # ensure the connection is clean.
  111
+      def reset!
  112
+        execute "ROLLBACK" rescue nil
  113
+      end
  114
+
106 115
       # Returns true if its safe to reload the connection between requests for development mode.
107 116
       # This is not the case for Ruby/MySQL and it's not necessary for any adapters except SQLite.
108 117
       def requires_reloading?
109 118
         false
110 119
       end
111 120
 
112  
-      # Lazily verify this connection, calling <tt>active?</tt> only if it hasn't
113  
-      # been called for +timeout+ seconds.
  121
+      # Lazily verify this connection, calling <tt>active?</tt> only if it
  122
+      # hasn't been called for +timeout+ seconds.
114 123
       def verify!(timeout)
115 124
         now = Time.now.to_i
116 125
         if (now - @last_verification) > timeout
9  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -280,6 +280,15 @@ def disconnect!
280 280
         @connection.close rescue nil
281 281
       end
282 282
 
  283
+      def reset!
  284
+        if @connection.respond_to?(:change_user)
  285
+          # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
  286
+          # reset the connection is to change the user to the same user.
  287
+          @connection.change_user(@config[:username], @config[:password], @config[:database])
  288
+        else
  289
+          super
  290
+        end
  291
+      end
283 292
 
284 293
       # DATABASE STATEMENTS ======================================
285 294
 

0 notes on commit a96b7d4

Please sign in to comment.
Something went wrong with that request. Please try again.