Skip to content

Commit 371e375

Browse files
authored
Merge pull request rails#33363 from ahorek/transaction_bug
use set_server_option if possible
2 parents dc1c679 + 425449e commit 371e375

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,14 +559,32 @@ def max_allowed_packet
559559
end
560560

561561
def with_multi_statements
562-
previous_flags = @config[:flags]
563-
@config[:flags] = Mysql2::Client::MULTI_STATEMENTS
564-
reconnect!
562+
if supports_set_server_option?
563+
@connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON)
564+
elsif !supports_multi_statements?
565+
previous_flags = @config[:flags]
566+
@config[:flags] = Mysql2::Client::MULTI_STATEMENTS
567+
reconnect!
568+
end
565569

566570
yield
567571
ensure
568-
@config[:flags] = previous_flags
569-
reconnect!
572+
unless supports_multi_statements?
573+
if supports_set_server_option?
574+
@connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF)
575+
else
576+
@config[:flags] = previous_flags
577+
reconnect!
578+
end
579+
end
580+
end
581+
582+
def supports_multi_statements?
583+
(@config[:flags] & Mysql2::Client::MULTI_STATEMENTS) != 0
584+
end
585+
586+
def supports_set_server_option?
587+
@connection.respond_to?(:set_server_option)
570588
end
571589

572590
def initialize_type_map(m = type_map)

activerecord/test/cases/fixtures_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ def test_bulk_insert_with_a_multi_statement_query_raises_an_exception_when_any_i
114114
end
115115
end
116116
end
117+
118+
def test_bulk_insert_with_a_multi_statement_query_in_a_nested_transaction
119+
fixtures = {
120+
"traffic_lights" => [
121+
{ "location" => "US", "state" => ["NY"], "long_state" => ["a"] },
122+
]
123+
}
124+
125+
ActiveRecord::Base.transaction do
126+
con = ActiveRecord::Base.connection
127+
assert_equal 1, con.open_transactions
128+
con.insert_fixtures_set(fixtures)
129+
assert_equal 1, con.open_transactions
130+
end
131+
end
117132
end
118133

119134
if current_adapter?(:Mysql2Adapter)

0 commit comments

Comments
 (0)