提交 4a65dfcb 编写于 作者: R Ryuta Kamizono

Raise `TransactionTimeout` when lock wait timeout exceeded for PG adapter

Follow up of #30360.
上级 ee5cf14a
......@@ -144,8 +144,8 @@
*Jeremy Green*
* Add new error class `TransactionTimeout` for MySQL adapter which will be raised
when lock wait time expires.
* Add new error class `TransactionTimeout` which will be raised
when lock wait timeout exceeded.
*Gabriel Courtemanche*
......
......@@ -391,6 +391,7 @@ def default_index_type?(index) # :nodoc:
UNIQUE_VIOLATION = "23505"
SERIALIZATION_FAILURE = "40001"
DEADLOCK_DETECTED = "40P01"
LOCK_NOT_AVAILABLE = "55P03"
def translate_exception(exception, message)
return exception unless exception.respond_to?(:result)
......@@ -410,6 +411,8 @@ def translate_exception(exception, message)
SerializationFailure.new(message)
when DEADLOCK_DETECTED
Deadlocked.new(message)
when LOCK_NOT_AVAILABLE
TransactionTimeout.new(message)
else
super
end
......
......@@ -60,7 +60,7 @@ class Sample < ActiveRecord::Base
end
end
test "raises TransactionTimeout when mysql raises ER_LOCK_WAIT_TIMEOUT" do
test "raises TransactionTimeout when lock wait timeout exceeded" do
assert_raises(ActiveRecord::TransactionTimeout) do
s = Sample.create!(value: 1)
latch1 = Concurrent::CountDownLatch.new
......
......@@ -91,6 +91,35 @@ class Sample < ActiveRecord::Base
end
end
test "raises TransactionTimeout when lock wait timeout exceeded" do
skip unless ActiveRecord::Base.connection.postgresql_version >= 90300
assert_raises(ActiveRecord::TransactionTimeout) do
s = Sample.create!(value: 1)
latch1 = Concurrent::CountDownLatch.new
latch2 = Concurrent::CountDownLatch.new
thread = Thread.new do
Sample.transaction do
Sample.lock.find(s.id)
latch1.count_down
latch2.wait
end
end
begin
Sample.transaction do
latch1.wait
Sample.connection.execute("SET lock_timeout = 1")
Sample.lock.find(s.id)
end
ensure
Sample.connection.execute("SET lock_timeout = DEFAULT")
latch2.count_down
thread.join
end
end
end
private
def with_warning_suppression
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册