Skip to content
Browse files

MySQL, PostgreSQL: reconnect! also reconfigures the connection. Other…

…wise, 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
  • Loading branch information...
1 parent d958f22 commit e8f664dde0364c84ff31537cc0b5b89683fbbca8 @jeremy jeremy committed Nov 22, 2005
View
2 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]
View
34 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -38,17 +38,7 @@ def self.mysql_connection(config) # :nodoc:
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 @@ class MysqlAdapter < AbstractAdapter
"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 @@ def quote_column_name(name) #:nodoc:
end
def quote_string(string) #:nodoc:
- @mysql.quote(string)
+ @connection.quote(string)
end
def quoted_true
@@ -170,7 +161,7 @@ def reconnect!
@connection.ping
else
@connection.close rescue nil
- @connection.real_connect(*@connection_options)
+ connect
end
end
@@ -318,6 +309,19 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
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)
View
21 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -12,7 +12,6 @@ def self.postgresql_connection(config) # :nodoc:
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 @@ def self.postgresql_connection(config) # :nodoc:
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 @@ def adapter_name
'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 @@ def reconnect!
# TODO: postgres-pr doesn't have PGconn#reset.
if @connection.respond_to?(:reset)
@connection.reset
+ configure_connection
end
end
@@ -327,6 +331,15 @@ def remove_index(table_name, options) #:nodoc:
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

0 comments on commit e8f664d

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