diff options
author | Kir Shatrov <shatrov@me.com> | 2019-07-16 20:08:50 +0100 |
---|---|---|
committer | Kir Shatrov <shatrov@me.com> | 2019-07-16 20:24:27 +0100 |
commit | 080939c98724b039704ca8115ba610134d1edcff (patch) | |
tree | c47ca5efeac488817c5074ad95c6310c5b3ee2c9 | |
parent | da4e18c5c2e965bfdfc442b92881326453de6f80 (diff) | |
download | rails-080939c98724b039704ca8115ba610134d1edcff.tar.gz rails-080939c98724b039704ca8115ba610134d1edcff.tar.bz2 rails-080939c98724b039704ca8115ba610134d1edcff.zip |
Raise specific exception on Mysql2::Error::TimeoutError
3 files changed, 23 insertions, 1 deletions
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 e9ae8d159e..405fecb603 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -619,7 +619,11 @@ module ActiveRecord when ER_QUERY_INTERRUPTED QueryCanceled.new(message, sql: sql, binds: binds) else - super + if exception.is_a?(Mysql2::Error::TimeoutError) + ActiveRecord::AdapterTimeout.new(message, sql: sql, binds: binds) + else + super + end end end diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb index c8c06375a3..012ff8c46f 100644 --- a/activerecord/lib/active_record/errors.rb +++ b/activerecord/lib/active_record/errors.rb @@ -365,6 +365,10 @@ module ActiveRecord class QueryCanceled < StatementInvalid end + # AdapterTimeout will be raised when database clients times out while waiting from the server + class AdapterTimeout < StatementInvalid + end + # UnknownAttributeReference is raised when an unknown and potentially unsafe # value is passed to a query method when allow_unsafe_raw_sql is set to # :disabled. For example, passing a non column name value to a relation's diff --git a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb index df84a40f63..6387f65b62 100644 --- a/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb +++ b/activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb @@ -225,6 +225,20 @@ class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase end end + def test_read_timeout_exception + ActiveRecord::Base.establish_connection( + ActiveRecord::Base.configurations[:arunit].merge("read_timeout" => 1) + ) + + error = assert_raises(ActiveRecord::AdapterTimeout) do + ActiveRecord::Base.connection.execute("SELECT SLEEP(2)") + end + + assert_equal Mysql2::Error::TimeoutError, error.cause.class + ensure + ActiveRecord::Base.establish_connection :arunit + end + private def with_example_table(definition = "id int auto_increment primary key, number int, data varchar(255)", &block) super(@conn, "ex", definition, &block) |