钱包余额增加悲观锁

This commit is contained in:
cano
2024-03-29 11:10:00 +08:00
parent 4b47fe7250
commit 81a95f47a0
3 changed files with 63 additions and 2 deletions

View File

@ -71,7 +71,7 @@ class BaseModel extends Model
return $this->newQuery()->where($aWhere)->get($col);
}
function setLockByPrimaryKey($id,$lock_type = self::LOCK_TYPE_FOR_UPDATE): Model|\Illuminate\Database\Eloquent\Builder|null
function findByPrimaryKeyWithLock($id,$lock_type = self::LOCK_TYPE_FOR_UPDATE): Model|\Illuminate\Database\Eloquent\Builder|null
{
$model = $this->newQuery()->where($this->primaryKey, $id);
if($lock_type == self::LOCK_TYPE_FOR_SHARE){
@ -82,6 +82,17 @@ class BaseModel extends Model
return $model->first();
}
function findByWhereWithLock($id,$col=['*'],$lock_type = self::LOCK_TYPE_FOR_UPDATE): Model|\Illuminate\Database\Eloquent\Builder|null
{
$model = $this->newQuery()->where($this->primaryKey, $id);
if($lock_type == self::LOCK_TYPE_FOR_SHARE){
$model = $model->sharedLock();
}elseif ($lock_type == self::LOCK_TYPE_FOR_UPDATE){
$model = $model->lockForUpdate();
}
return $model->first($col);
}
}