Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f5c0c7ff
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f5c0c7ff
编写于
5月 22, 2016
作者:
E
Erol Fornoles
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce new ActiveRecord transaction error classes
上级
61483b18
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
57 addition
and
26 deletion
+57
-26
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+1
-1
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+4
-1
activerecord/lib/active_record/errors.rb
activerecord/lib/active_record/errors.rb
+12
-2
activerecord/test/cases/adapters/mysql2/transaction_test.rb
activerecord/test/cases/adapters/mysql2/transaction_test.rb
+13
-20
activerecord/test/cases/adapters/postgresql/transaction_test.rb
...record/test/cases/adapters/postgresql/transaction_test.rb
+27
-2
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
f5c0c7ff
...
...
@@ -742,7 +742,7 @@ def translate_exception(exception, message)
when
ER_DATA_TOO_LONG
ValueTooLong
.
new
(
message
)
when
ER_LOCK_DEADLOCK
TransactionSerializationError
.
new
(
message
)
DeadlockDetected
.
new
(
message
)
else
super
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
f5c0c7ff
...
...
@@ -407,6 +407,7 @@ def postgresql_version
FOREIGN_KEY_VIOLATION
=
"23503"
UNIQUE_VIOLATION
=
"23505"
SERIALIZATION_FAILURE
=
"40001"
DEADLOCK_DETECTED
=
"40P01"
def
translate_exception
(
exception
,
message
)
return
exception
unless
exception
.
respond_to?
(
:result
)
...
...
@@ -419,7 +420,9 @@ def translate_exception(exception, message)
when
VALUE_LIMIT_VIOLATION
ValueTooLong
.
new
(
message
)
when
SERIALIZATION_FAILURE
TransactionSerializationError
.
new
(
message
)
SerializationFailure
.
new
(
message
)
when
DEADLOCK_DETECTED
DeadlockDetected
.
new
(
message
)
else
super
end
...
...
activerecord/lib/active_record/errors.rb
浏览文件 @
f5c0c7ff
...
...
@@ -285,14 +285,24 @@ class ImmutableRelation < ActiveRecordError
class
TransactionIsolationError
<
ActiveRecordError
end
# Transaction
Serialization
Error will be raised when a transaction is rolled
# Transaction
Rollback
Error will be raised when a transaction is rolled
# back by the database due to a serialization failure or a deadlock.
#
# See the following:
#
# * http://www.postgresql.org/docs/current/static/transaction-iso.html
# * https://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html#error_er_lock_deadlock
class
TransactionSerializationError
<
ActiveRecordError
class
TransactionRollbackError
<
ActiveRecordError
end
# SerializationFailure will be raised when a transaction is rolled
# back by the database due to a serialization failure.
class
SerializationFailure
<
TransactionRollbackError
end
# DeadlockDetected will be raised when a transaction is rolled
# back by the database when a deadlock is encountered.
class
DeadlockDetected
<
TransactionRollbackError
end
# IrreversibleOrderError is raised when a relation's order is too complex for
...
...
activerecord/test/cases/adapters/mysql2/transaction_test.rb
浏览文件 @
f5c0c7ff
...
...
@@ -27,32 +27,25 @@ class Sample < ActiveRecord::Base
@connection
.
drop_table
'samples'
,
if_exists:
true
end
test
"raises error when a serialization failure occurs"
do
assert_raises
(
ActiveRecord
::
TransactionSerializationError
)
do
thread
=
Thread
.
new
do
Sample
.
transaction
isolation: :serializable
do
Sample
.
delete_all
10
.
times
do
|
i
|
sleep
0.1
test
"raises DeadlockDetected when a deadlock is encountered"
do
assert_raises
(
ActiveRecord
::
DeadlockDetected
)
do
s1
=
Sample
.
create
value:
1
s2
=
Sample
.
create
value:
2
Sample
.
create
value:
i
end
thread
=
Thread
.
new
do
Sample
.
transaction
do
s1
.
lock!
sleep
1
s2
.
update_attributes
value:
1
end
end
sleep
0.1
Sample
.
transaction
isolation: :serializable
do
Sample
.
delete_all
10
.
times
do
|
i
|
sleep
0.1
Sample
.
create
value:
i
end
sleep
0.5
Sample
.
transaction
do
s2
.
lock!
sleep
1
s1
.
update_attributes
value:
2
end
thread
.
join
...
...
activerecord/test/cases/adapters/postgresql/transaction_test.rb
浏览文件 @
f5c0c7ff
...
...
@@ -26,9 +26,9 @@ class Sample < ActiveRecord::Base
@connection
.
drop_table
'samples'
,
if_exists:
true
end
test
"raises
error
when a serialization failure occurs"
do
test
"raises
SerializationFailure
when a serialization failure occurs"
do
with_warning_suppression
do
assert_raises
(
ActiveRecord
::
TransactionSerializationError
)
do
assert_raises
(
ActiveRecord
::
SerializationFailure
)
do
thread
=
Thread
.
new
do
Sample
.
transaction
isolation: :serializable
do
Sample
.
delete_all
...
...
@@ -51,8 +51,33 @@ class Sample < ActiveRecord::Base
Sample
.
create
value:
i
end
end
thread
.
join
end
end
end
test
"raises DeadlockDetected when a deadlock is encountered"
do
with_warning_suppression
do
assert_raises
(
ActiveRecord
::
DeadlockDetected
)
do
s1
=
Sample
.
create
value:
1
s2
=
Sample
.
create
value:
2
thread
=
Thread
.
new
do
Sample
.
transaction
do
s1
.
lock!
sleep
1
s2
.
update_attributes
value:
1
end
end
sleep
0.5
Sample
.
transaction
do
s2
.
lock!
sleep
1
s1
.
update_attributes
value:
2
end
thread
.
join
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录