Skip to content

Commit

Permalink
Mysql#reconnect is set according to the 'reconnect' key in the connec…
Browse files Browse the repository at this point in the history
…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>
[rails#1797 state:committed]
  • Loading branch information
dubek authored and NZKoz committed Jan 26, 2009
1 parent b081059 commit e5c211c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Expand Up @@ -150,6 +150,7 @@ def missing_default_forged_as_empty_string?(default)
# * <tt>:password</tt> - Defaults to nothing.
# * <tt>:database</tt> - The name of the database. No default, must be provided.
# * <tt>:encoding</tt> - (Optional) Sets the client encoding by executing "SET NAMES <encoding>" after connection.
# * <tt>:reconnect</tt> - Defaults to false (See MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
# * <tt>:sslca</tt> - Necessary to use MySQL with an SSL connection.
# * <tt>:sslkey</tt> - Necessary to use MySQL with an SSL connection.
# * <tt>:sslcert</tt> - Necessary to use MySQL with an SSL connection.
Expand Down Expand Up @@ -534,8 +535,6 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)

private
def connect
@connection.reconnect = true if @connection.respond_to?(:reconnect=)

encoding = @config[:encoding]
if encoding
@connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
Expand All @@ -546,6 +545,10 @@ def connect
end

@connection.real_connect(*@connection_options)

# reconnect must be set after real_connect is called, because real_connect sets it to false internally
@connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)

configure_connection
end

Expand Down
26 changes: 26 additions & 0 deletions activerecord/test/cases/connection_test_mysql.rb
Expand Up @@ -2,9 +2,24 @@

class MysqlConnectionTest < ActiveRecord::TestCase
def setup
super
@connection = ActiveRecord::Base.connection
end

def test_mysql_reconnect_attribute_after_connection_with_reconnect_true
run_without_connection do |orig_connection|
ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => true}))
assert ActiveRecord::Base.connection.raw_connection.reconnect
end
end

def test_mysql_reconnect_attribute_after_connection_with_reconnect_false
run_without_connection do |orig_connection|
ActiveRecord::Base.establish_connection(orig_connection.merge({:reconnect => false}))
assert !ActiveRecord::Base.connection.raw_connection.reconnect
end
end

def test_no_automatic_reconnection_after_timeout
assert @connection.active?
@connection.update('set @@wait_timeout=1')
Expand All @@ -27,4 +42,15 @@ def test_successful_reconnection_after_timeout_with_verify
@connection.verify!
assert @connection.active?
end

private

def run_without_connection
original_connection = ActiveRecord::Base.remove_connection
begin
yield original_connection
ensure
ActiveRecord::Base.establish_connection(original_connection)
end
end
end

0 comments on commit e5c211c

Please sign in to comment.