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
This commit is contained in:
Jeremy Kemper
2005-11-22 23:55:04 +00:00
parent d958f22ecc
commit e8f664dde0
3 changed files with 38 additions and 19 deletions

View File

@@ -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 File

@@ -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)

View File

@@ -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