Skip to content

Commit

Permalink
Free MySQL::Result objects after a call to execute [#1416 state:resol…
Browse files Browse the repository at this point in the history
…ved]

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>
  • Loading branch information
Manfred authored and lifo committed Dec 18, 2008
1 parent 9c7fe7c commit 8326b95
Showing 1 changed file with 13 additions and 4 deletions.
Expand Up @@ -308,6 +308,7 @@ def select_rows(sql, name = nil)
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:

This comment has been minimized.

Copy link
@johanlunds

johanlunds Dec 21, 2008

Contributor

Remove the “nodoc” comment here?

log(sql, name) { @connection.query(sql) }
rescue ActiveRecord::StatementInvalid => exception
Expand Down Expand Up @@ -414,7 +415,9 @@ def collation

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

Expand All @@ -425,7 +428,8 @@ def drop_table(table_name, options = {})
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]
Expand All @@ -434,13 +438,16 @@ def indexes(table_name, name = nil)#:nodoc:

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

Expand Down Expand Up @@ -521,9 +528,11 @@ def show_variable(name)
# 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

Expand Down

1 comment on commit 8326b95

@Manfred
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all the adapters could use some documentation on database specific issues. Maybe that’s a good christmas vacation project?

Please sign in to comment.