From 8326b95169ae6af3b81f5596107fef9db4bcbbb0 Mon Sep 17 00:00:00 2001 From: Manfred Stienstra Date: Wed, 19 Nov 2008 15:17:40 +0100 Subject: Free MySQL::Result objects after a call to execute [#1416 state:resolved] No freeing Result objects causes the MySQL driver to free result sets at undefined times, this can lead to erratic performance in your application. Signed-off-by: Frederick Cheung --- .../active_record/connection_adapters/mysql_adapter.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index c3cbdc8c2f..46d4b6c89c 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -308,6 +308,7 @@ module ActiveRecord rows end + # Executes a SQL query and returns a MySQL::Result object. Note that you have to free the Result object after you're done using it. def execute(sql, name = nil) #:nodoc: log(sql, name) { @connection.query(sql) } rescue ActiveRecord::StatementInvalid => exception @@ -414,7 +415,9 @@ module ActiveRecord def tables(name = nil) #:nodoc: tables = [] - execute("SHOW TABLES", name).each { |field| tables << field[0] } + result = execute("SHOW TABLES", name) + result.each { |field| tables << field[0] } + result.free tables end @@ -425,7 +428,8 @@ module ActiveRecord def indexes(table_name, name = nil)#:nodoc: indexes = [] current_index = nil - execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name).each do |row| + result = execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name) + result.each do |row| if current_index != row[2] next if row[2] == "PRIMARY" # skip the primary key current_index = row[2] @@ -434,13 +438,16 @@ module ActiveRecord indexes.last.columns << row[4] end + result.free indexes end def columns(table_name, name = nil)#:nodoc: sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}" columns = [] - execute(sql, name).each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") } + result = execute(sql, name) + result.each { |field| columns << MysqlColumn.new(field[0], field[4], field[1], field[2] == "YES") } + result.free columns end @@ -521,9 +528,11 @@ module ActiveRecord # Returns a table's primary key and belonging sequence. def pk_and_sequence_for(table) #:nodoc: keys = [] - execute("describe #{quote_table_name(table)}").each_hash do |h| + result = execute("describe #{quote_table_name(table)}") + result.each_hash do |h| keys << h["Field"]if h["Key"] == "PRI" end + result.free keys.length == 1 ? [keys.first, nil] : nil end -- cgit v1.2.3