aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorManfred Stienstra <manfred@fngtps.com>2008-11-19 15:17:40 +0100
committerPratik Naik <pratiknaik@gmail.com>2008-12-18 19:19:36 +0000
commit8326b95169ae6af3b81f5596107fef9db4bcbbb0 (patch)
tree269f82deb7a37eb5bf2ea9efc7b9942a832c18ff /activerecord
parent9c7fe7c6729bbdde4a22f58e7279463092390330 (diff)
downloadrails-8326b95169ae6af3b81f5596107fef9db4bcbbb0.tar.gz
rails-8326b95169ae6af3b81f5596107fef9db4bcbbb0.tar.bz2
rails-8326b95169ae6af3b81f5596107fef9db4bcbbb0.zip
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 <frederick.cheung@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb17
1 files changed, 13 insertions, 4 deletions
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