Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
84e541ec
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,发现更多精彩内容 >>
提交
84e541ec
编写于
5月 17, 2011
作者:
S
Sebastian Martinez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Better doc styling in ActiveRecord::Locking
上级
110a066b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
17 addition
and
17 deletion
+17
-17
activerecord/lib/active_record/locking/optimistic.rb
activerecord/lib/active_record/locking/optimistic.rb
+15
-15
activerecord/lib/active_record/locking/pessimistic.rb
activerecord/lib/active_record/locking/pessimistic.rb
+2
-2
未找到文件。
activerecord/lib/active_record/locking/optimistic.rb
浏览文件 @
84e541ec
...
...
@@ -3,16 +3,17 @@ module Locking
# == What is Optimistic Locking
#
# Optimistic locking allows multiple users to access the same record for edits, and assumes a minimum of
# conflicts with the data. It does this by checking whether another process has made changes to a record since
# it was opened, an ActiveRecord::StaleObjectError is thrown if that has occurred and the update is ignored.
# conflicts with the data. It does this by checking whether another process has made changes to a record since
# it was opened, an <tt>ActiveRecord::StaleObjectError</tt> exception is thrown if that has occurred
# and the update is ignored.
#
# Check out
ActiveRecord::Locking::Pessimistic
for an alternative.
# Check out
<tt>ActiveRecord::Locking::Pessimistic</tt>
for an alternative.
#
# == Usage
#
# Active Records support optimistic locking if the field
<tt>lock_version</tt> is present.
Each update to the
# record increments the
lock_version
column and the locking facilities ensure that records instantiated twice
# will let the last one saved raise a
StaleObjectError
if the first was also updated. Example:
# Active Records support optimistic locking if the field
+lock_version+ is present.
Each update to the
# record increments the
+lock_version+
column and the locking facilities ensure that records instantiated twice
# will let the last one saved raise a
+StaleObjectError+
if the first was also updated. Example:
#
# p1 = Person.find(1)
# p2 = Person.find(1)
...
...
@@ -36,10 +37,10 @@ module Locking
# You're then responsible for dealing with the conflict by rescuing the exception and either rolling back, merging,
# or otherwise apply the business logic needed to resolve the conflict.
#
# You must ensure that your database schema defaults the
lock_version
column to 0.
# You must ensure that your database schema defaults the
+lock_version+
column to 0.
#
# This behavior can be turned off by setting <tt>ActiveRecord::Base.lock_optimistically = false</tt>.
# To override the name of the
lock_version
column, invoke the <tt>set_locking_column</tt> method.
# To override the name of the
+lock_version+
column, invoke the <tt>set_locking_column</tt> method.
# This method uses the same syntax as <tt>set_table_name</tt>
module
Optimistic
extend
ActiveSupport
::
Concern
...
...
@@ -68,9 +69,9 @@ def attributes_from_column_definition
result
=
super
# If the locking column has no default value set,
# start the lock version at zero.
Note we can't use
#
locking_enabled? at this point as @attributes may
#
not have been initialized yet
# start the lock version at zero. Note we can't use
#
<tt>locking_enabled?</tt> at this point as
#
<tt>@attributes</tt> may not have been initialized yet.
if
lock_optimistically
&&
result
.
include?
(
self
.
class
.
locking_column
)
result
[
self
.
class
.
locking_column
]
||=
0
...
...
@@ -137,10 +138,9 @@ def destroy #:nodoc:
module
ClassMethods
DEFAULT_LOCKING_COLUMN
=
'lock_version'
# Is optimistic locking enabled for this table? Returns true if the
# +lock_optimistically+ flag is set to true (which it is, by default)
# and the table includes the +locking_column+ column (defaults to
# +lock_version+).
# Returns true if the +lock_optimistically+ flag is set to true
# (which it is, by default) and the table includes the
# +locking_column+ column (defaults to +lock_version+).
def
locking_enabled?
lock_optimistically
&&
columns_hash
[
locking_column
]
end
...
...
activerecord/lib/active_record/locking/pessimistic.rb
浏览文件 @
84e541ec
...
...
@@ -3,7 +3,7 @@ module Locking
# Locking::Pessimistic provides support for row-level locking using
# SELECT ... FOR UPDATE and other lock types.
#
# Pass <tt>:lock => true</tt> to
ActiveRecord::Base.find
to obtain an exclusive
# Pass <tt>:lock => true</tt> to
<tt>ActiveRecord::Base.find</tt>
to obtain an exclusive
# lock on the selected rows:
# # select * from accounts where id=1 for update
# Account.find(1, :lock => true)
...
...
@@ -21,7 +21,7 @@ module Locking
# yuko.save!
# end
#
# You can also use
ActiveRecord::Base#lock!
method to lock one record by id.
# You can also use
<tt>ActiveRecord::Base#lock!</tt>
method to lock one record by id.
# This may be better if you don't need to lock every row. Example:
#
# Account.transaction do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录