Skip to content
This repository
Browse code

Mysql#reconnect is set according to the 'reconnect' key in the connec…

…tion spec.

The 'reconenct' boolean option is read from the connection specification
and is used to set the reconnect attribute of Mysql.  The default is
false in order not to change existing application behaviour.

Also, reconnect is set AFTER real_connect is called, so its value sticks
(the mysql gem sets reconnect to false inside real_connect).

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1797 state:committed]
  • Loading branch information...
commit 5fe6635e05a18b312c47fe6bbbaf88fd62e7703d 1 parent 57f0b85
Dov Murik authored January 26, 2009 NZKoz committed January 27, 2009
7  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -152,6 +152,7 @@ def missing_default_forged_as_empty_string?(default)
152 152
     # * <tt>:password</tt> - Defaults to nothing.
153 153
     # * <tt>:database</tt> - The name of the database. No default, must be provided.
154 154
     # * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
  155
+    # * <tt>:reconnect</tt> - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
155 156
     # * <tt>:sslca</tt> - Necessary to use MySQL with an SSL connection.
156 157
     # * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
157 158
     # * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
@@ -563,8 +564,6 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
563 564
 
564 565
       private
565 566
         def connect
566  
-          @connection.reconnect = true if @connection.respond_to?(:reconnect=)
567  
-
568 567
           encoding = @config[:encoding]
569 568
           if encoding
570 569
             @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
@@ -575,6 +574,10 @@ def connect
575 574
           end
576 575
 
577 576
           @connection.real_connect(*@connection_options)
  577
+
  578
+          # reconnect must be set after real_connect is called, because real_connect sets it to false internally
  579
+          @connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)
  580
+
578 581
           configure_connection
579 582
         end
580 583
 
26  activerecord/test/cases/connection_test_mysql.rb
@@ -2,9 +2,24 @@
2 2
 
3 3
 class MysqlConnectionTest < ActiveRecord::TestCase
4 4
   def setup
  5
+    super
5 6
     @connection = ActiveRecord::Base.connection
6 7
   end
7 8
 
  9
+  def test_mysql_reconnect_attribute_after_connection_with_reconnect_true
  10
+    run_without_connection do |orig_connection|
  11
+      ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => true}))
  12
+      assert ActiveRecord::Base.connection.raw_connection.reconnect
  13
+    end
  14
+  end
  15
+
  16
+  def test_mysql_reconnect_attribute_after_connection_with_reconnect_false
  17
+    run_without_connection do |orig_connection|
  18
+      ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => false}))
  19
+      assert !ActiveRecord::Base.connection.raw_connection.reconnect
  20
+    end
  21
+  end
  22
+
8 23
   def test_no_automatic_reconnection_after_timeout
9 24
     assert @connection.active?
10 25
     @connection.update('set @@wait_timeout=1')
@@ -27,4 +42,15 @@ def test_successful_reconnection_after_timeout_with_verify
27 42
     @connection.verify!
28 43
     assert @connection.active?
29 44
   end
  45
+
  46
+  private
  47
+
  48
+  def run_without_connection
  49
+    original_connection = ActiveRecord::Base.remove_connection
  50
+    begin
  51
+      yield original_connection
  52
+    ensure
  53
+      ActiveRecord::Base.establish_connection(original_connection)
  54
+    end
  55
+  end
30 56
 end

0 notes on commit 5fe6635

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