请教关于 sharedLock 和 lockForUpdate 的区别
version:laravel 5.6
路径:Illuminate\Database\Query\Builder
/**
* Lock the selected rows in the table.
*
* @param string|bool $value
* @return $this
*/
public function lock($value = true)
{
$this->lock = $value;
if (! is_null($this->lock)) {
$this->useWritePdo();
}
return $this;
}
/**
* Lock the selected rows in the table for updating.
*
* @return \Illuminate\Database\Query\Builder
*/
public function lockForUpdate()
{
return $this->lock(true);
}
/**
* Share lock the selected rows in the table.
*
* @return \Illuminate\Database\Query\Builder
*/
public function sharedLock()
{
return $this->lock(false);
}
请问lockForUpdate 和 sharedLock的区别?function lock
里使用了is_null($this->lock)
做判断的,都会运行$this->useWritePdo()
在laravel 5.3(含)
前使用的是
public function lock($value = true)
{
$this->lock = $value;
if ($this->lock) {
$this->useWritePdo();
}
return $this;
}
由于这个Issue$this->useWritePdo()的if
判断条件被修改了.
感谢回复
lockForUpdate 比 sharedLock 严格。
sharedLock,分享锁。别的事务可以读,但不能写,只能 sharedLock 更新完再写。
lockForUpdate,更新锁。其他的都不能读写,只能 lockForUpdate 更新完再读写。
没用过,只是根据已有的阅读知识的理解。
两者的区别在于:当两个
transaction(事务)
都在运行.如果先运行的使用了
LockForUpdate
,后者在前一个transaction
commit
前无法选中同一条数据(要等前者跑完).如果先运行的使用了
sharedLock
,后运行的即使在commit
前也可以选中数据