1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Improve detection of ActiveRecord::StatementTimeout with mysql2 adapter

in the edge case when the query is terminated by MySQL server during filesort.
See https://bugs.mysql.com/bug.php?id=96537 for more details.
This commit is contained in:
Kir Shatrov 2019-08-14 11:51:04 +01:00
parent 8ab4fd12f1
commit c99c572d37
3 changed files with 21 additions and 1 deletions

View file

@ -1,3 +1,7 @@
* Improve detection of ActiveRecord::StatementTimeout with mysql2 adapter in the edge case when the query is terminated during filesort.
*Kir Shatrov*
* Stop trying to read yaml file fixtures when loading Active Record fixtures.
*Gannon McGibbon*

View file

@ -574,6 +574,7 @@ module ActiveRecord
# See https://dev.mysql.com/doc/refman/5.7/en/server-error-reference.html
ER_DB_CREATE_EXISTS = 1007
ER_FILSORT_ABORT = 1028
ER_DUP_ENTRY = 1062
ER_NOT_NULL_VIOLATION = 1048
ER_NO_REFERENCED_ROW = 1216
@ -617,7 +618,7 @@ module ActiveRecord
Deadlocked.new(message, sql: sql, binds: binds)
when ER_LOCK_WAIT_TIMEOUT
LockWaitTimeout.new(message, sql: sql, binds: binds)
when ER_QUERY_TIMEOUT
when ER_QUERY_TIMEOUT, ER_FILSORT_ABORT
StatementTimeout.new(message, sql: sql, binds: binds)
when ER_QUERY_INTERRUPTED
QueryCanceled.new(message, sql: sql, binds: binds)

View file

@ -240,6 +240,21 @@ class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase
ActiveRecord::Base.establish_connection :arunit
end
def test_statement_timeout_error_codes
raw_conn = @conn.raw_connection
assert_raises(ActiveRecord::StatementTimeout) do
raw_conn.stub(:query, ->(_sql) { raise Mysql2::Error.new("fail", 50700, ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::ER_FILSORT_ABORT) }) {
@conn.execute("SELECT 1")
}
end
assert_raises(ActiveRecord::StatementTimeout) do
raw_conn.stub(:query, ->(_sql) { raise Mysql2::Error.new("fail", 50700, ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::ER_QUERY_TIMEOUT) }) {
@conn.execute("SELECT 1")
}
end
end
private
def with_example_table(definition = "id int auto_increment primary key, number int, data varchar(255)", &block)
super(@conn, "ex", definition, &block)