Skip to content

Commit

Permalink
Add TransactionTimeout for MySQL error code 1205
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Courtemanche committed Aug 22, 2017
1 parent 39e6eea commit c5edd97
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
Expand Up @@ -630,6 +630,7 @@ def add_options_for_index_columns(quoted_columns, **options)
ER_LOCK_DEADLOCK = 1213
ER_CANNOT_ADD_FOREIGN = 1215
ER_CANNOT_CREATE_TABLE = 1005
ER_LOCK_WAIT_TIMEOUT = 1205

def translate_exception(exception, message)
case error_number(exception)
Expand All @@ -653,6 +654,8 @@ def translate_exception(exception, message)
NotNullViolation.new(message)
when ER_LOCK_DEADLOCK
Deadlocked.new(message)
when ER_LOCK_WAIT_TIMEOUT
TransactionTimeout.new(message)
else
super
end
Expand Down
5 changes: 5 additions & 0 deletions activerecord/lib/active_record/errors.rb
Expand Up @@ -334,4 +334,9 @@ class Deadlocked < TransactionRollbackError
# +reverse_order+ to automatically reverse.
class IrreversibleOrderError < ActiveRecordError
end

# TransactionTimeout will be raised when lock wait timeout expires.
# Wait time value is set by innodb_lock_wait_timeout.
class TransactionTimeout < StatementInvalid
end
end
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/mysql2/transaction_test.rb
Expand Up @@ -59,5 +59,11 @@ class Sample < ActiveRecord::Base
end
end
end

test "raises TransactionTimeout when mysql raises ER_LOCK_WAIT_TIMEOUT" do
assert_raises(ActiveRecord::TransactionTimeout) do
ActiveRecord::Base.connection.execute("SIGNAL SQLSTATE 'HY000' SET MESSAGE_TEXT = 'Testing error', MYSQL_ERRNO = 1205;")
end
end
end
end

0 comments on commit c5edd97

Please sign in to comment.