aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-11-22 23:55:04 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-11-22 23:55:04 +0000
commite8f664dde0364c84ff31537cc0b5b89683fbbca8 (patch)
tree45c240319dae228ab6359e604cb6a03f7652770f
parentd958f22ecc47f34aced7f5619be483d9686a26f0 (diff)
downloadrails-e8f664dde0364c84ff31537cc0b5b89683fbbca8.tar.gz
rails-e8f664dde0364c84ff31537cc0b5b89683fbbca8.tar.bz2
rails-e8f664dde0364c84ff31537cc0b5b89683fbbca8.zip
MySQL, PostgreSQL: reconnect! also reconfigures the connection. Otherwise, the connection 'loses' its settings if it times out and is reconnected. Closes #2978.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3165 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/mysql_adapter.rb34
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb21
3 files changed, 38 insertions, 19 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index f3c2a4ea7c..c5116d6d9e 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* MySQL, PostgreSQL: reconnect! also reconfigures the connection. Otherwise, the connection 'loses' its settings if it times out and is reconnected. #2978 [Shugo Maeda]
+
* has_and_belongs_to_many: use JOIN instead of LEFT JOIN. [Jeremy Kemper]
* MySQL: introduce :encoding option to specify the character set for client, connection, and results. Only available for MySQL 4.1 and later with the mysql-ruby driver. Do SHOW CHARACTER SET in mysql client to see available encodings. #2975 [Shugo Maeda]
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 177d201259..76ea6e6530 100755
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -38,17 +38,7 @@ module ActiveRecord
mysql = Mysql.init
mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey]
- if config[:encoding]
- begin
- mysql.options(Mysql::SET_CHARSET_NAME, config[:encoding])
- rescue
- raise ActiveRecord::ConnectionFailed, 'The :encoding option is only available for MySQL 4.1 and later with the mysql-ruby driver. Again, this does not work with the ruby-mysql driver or MySQL < 4.1.'
- end
- end
-
- conn = mysql.real_connect(host, username, password, database, port, socket)
- conn.query("SET NAMES '#{config[:encoding]}'") if config[:encoding]
- ConnectionAdapters::MysqlAdapter.new(conn, logger, [host, username, password, database, port, socket], mysql)
+ ConnectionAdapters::MysqlAdapter.new(mysql, logger, [host, username, password, database, port, socket], config)
end
end
@@ -97,10 +87,11 @@ module ActiveRecord
"MySQL server has gone away"
]
- def initialize(connection, logger, connection_options=nil, mysql=Mysql)
+ def initialize(connection, logger, connection_options=nil, config={})
super(connection, logger)
@connection_options = connection_options
- @mysql = mysql
+ @config = config
+ connect
end
def adapter_name #:nodoc:
@@ -144,7 +135,7 @@ module ActiveRecord
end
def quote_string(string) #:nodoc:
- @mysql.quote(string)
+ @connection.quote(string)
end
def quoted_true
@@ -170,7 +161,7 @@ module ActiveRecord
@connection.ping
else
@connection.close rescue nil
- @connection.real_connect(*@connection_options)
+ connect
end
end
@@ -318,6 +309,19 @@ module ActiveRecord
private
+ def connect
+ encoding = @config[:encoding]
+ if encoding
+ begin
+ @connection.options(Mysql::SET_CHARSET_NAME, encoding)
+ rescue
+ raise ActiveRecord::ConnectionFailed, 'The :encoding option is only available for MySQL 4.1 and later with the mysql-ruby driver. Again, this does not work with the ruby-mysql driver or MySQL < 4.1.'
+ end
+ end
+ @connection.real_connect(*@connection_options)
+ execute("SET NAMES '#{encoding}'") if encoding
+ end
+
def select(sql, name = nil)
@connection.query_with_result = true
result = execute(sql, name)
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index e1793ba460..3f826edfe2 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -12,7 +12,6 @@ module ActiveRecord
username = config[:username].to_s
password = config[:password].to_s
- encoding = config[:encoding]
min_messages = config[:min_messages]
if config.has_key?(:database)
@@ -22,12 +21,10 @@ module ActiveRecord
end
pga = ConnectionAdapters::PostgreSQLAdapter.new(
- PGconn.connect(host, port, "", "", database, username, password), logger
+ PGconn.connect(host, port, "", "", database, username, password), logger, config
)
pga.schema_search_path = config[:schema_search_path] || config[:schema_order]
- pga.execute("SET client_encoding TO '#{encoding}'") if encoding
- pga.execute("SET client_min_messages TO '#{min_messages}'") if min_messages
pga
end
@@ -52,6 +49,12 @@ module ActiveRecord
'PostgreSQL'
end
+ def initialize(connection, logger, config = {})
+ super(connection, logger)
+ @config = config
+ configure_connection
+ end
+
# Is this connection alive and ready for queries?
def active?
# TODO: postgres-pr doesn't have PGconn#status.
@@ -67,6 +70,7 @@ module ActiveRecord
# TODO: postgres-pr doesn't have PGconn#reset.
if @connection.respond_to?(:reset)
@connection.reset
+ configure_connection
end
end
@@ -327,6 +331,15 @@ module ActiveRecord
private
BYTEA_COLUMN_TYPE_OID = 17
+ def configure_connection
+ if @config[:encoding]
+ execute("SET client_encoding TO '#{@config[:encoding]}'")
+ end
+ if @config[:min_messages]
+ execute("SET client_min_messages TO '#{@config[:min_messages]}'")
+ end
+ end
+
def last_insert_id(table, sequence_name)
Integer(select_value("SELECT currval('#{sequence_name}')"))
end