Skip to content

Commit

Permalink
Bug Fix -- clean up connection after stored procedure [#3151 state:re…
Browse files Browse the repository at this point in the history
…solved]
  • Loading branch information
Jeff-Lawson authored and tenderlove committed Aug 17, 2010
1 parent 137e4e7 commit 7ce1539
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
Expand Up @@ -275,6 +275,7 @@ def select_rows(sql, name = nil)
rows = []
result.each { |row| rows << row }
result.free
@connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
rows
end

Expand Down Expand Up @@ -617,6 +618,7 @@ def select(sql, name = nil)
result = execute(sql, name)
rows = []
result.each_hash { |row| rows << row }
@connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
result.free
rows
end
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/cases/adapters/mysql/connection_test.rb
Expand Up @@ -48,6 +48,7 @@ def test_successful_reconnection_after_timeout_with_verify
def test_multi_results
rows = ActiveRecord::Base.connection.select_rows('CALL ten();')
assert_equal 10, rows[0][0].to_i, "ten() did not return 10 as expected: #{rows.inspect}"
assert @connection.active?, "Bad connection use by 'MysqlAdapter.select_rows'"
end
end

Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/adapters/mysql/sp_test.rb
@@ -0,0 +1,15 @@
require "cases/helper"
require 'models/topic'

class StoredProcedureTest < ActiveRecord::TestCase
fixtures :topics

# Test that MySQL allows multiple results for stored procedures
if Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
def test_multi_results_from_find_by_sql
topics = Topic.find_by_sql 'CALL topics();'
assert_equal 1, topics.size
assert ActiveRecord::Base.connection.active?, "Bad connection use by 'MysqlAdapter.select'"
end
end
end
11 changes: 11 additions & 0 deletions activerecord/test/schema/mysql_specific_schema.rb
Expand Up @@ -19,6 +19,17 @@
BEGIN
select 10;
END
SQL

ActiveRecord::Base.connection.execute <<-SQL
DROP PROCEDURE IF EXISTS topics;
SQL

ActiveRecord::Base.connection.execute <<-SQL
CREATE PROCEDURE topics() SQL SECURITY INVOKER
BEGIN
select * from topics limit 1;
END
SQL

end

0 comments on commit 7ce1539

Please sign in to comment.