diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 07acb5425e..07206f0d01 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -559,14 +559,32 @@ module ActiveRecord end def with_multi_statements - previous_flags = @config[:flags] - @config[:flags] = Mysql2::Client::MULTI_STATEMENTS - reconnect! + if supports_set_server_option? + @connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON) + elsif !supports_multi_statements? + previous_flags = @config[:flags] + @config[:flags] = Mysql2::Client::MULTI_STATEMENTS + reconnect! + end yield ensure - @config[:flags] = previous_flags - reconnect! + unless supports_multi_statements? + if supports_set_server_option? + @connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF) + else + @config[:flags] = previous_flags + reconnect! + end + end + end + + def supports_multi_statements? + (@config[:flags] & Mysql2::Client::MULTI_STATEMENTS) != 0 + end + + def supports_set_server_option? + @connection.respond_to?(:set_server_option) end def initialize_type_map(m = type_map) diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 2ead2c4816..639627852d 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -114,6 +114,21 @@ class FixturesTest < ActiveRecord::TestCase end end end + + def test_bulk_insert_with_a_multi_statement_query_in_a_nested_transaction + fixtures = { + "traffic_lights" => [ + { "location" => "US", "state" => ["NY"], "long_state" => ["a"] }, + ] + } + + ActiveRecord::Base.transaction do + con = ActiveRecord::Base.connection + assert_equal 1, con.open_transactions + con.insert_fixtures_set(fixtures) + assert_equal 1, con.open_transactions + end + end end if current_adapter?(:Mysql2Adapter)