public function testTrans(){
$time = date('H:i:s');
$model = new User();
$model->startTrans();
try{
$user = $model->lock(true)->where('id', 2)->find();
echo $user['pay'].'_'.date('H:i:s').PHP_EOL;
$user->update(['pay' => $user['pay'] + 10], ['id' => 2]);
sleep(10);
$model->commit();
return $time.'_lock_true_'.date('H:i:s');
} catch (\Exception $e) {
$model->rollback();
throw $e;
}
return $time.'_lock_false_'.date('H:i:s');
}
public function testTransSync(){
$time = date('H:i:s');
$model = new User();
$model->startTrans();
try{
$user = $model->lock(true)->where('id', 2)->find();
echo $user['pay'].'_'.date('H:i:s').PHP_EOL;
$user->update(['pay' => $user['pay'] + 10], ['id' => 2]);
sleep(10);
$model->commit();
return $time.'_lock_true_'.date('H:i:s');
} catch (\Exception $e) {
$model->rollback();
throw $e;
}
return $time.'_lock_false_'.date('H:i:s');
}
上面是加锁的版本, 不加锁就是把->lock(true)去掉。加锁的测试结果: $user['pay'] == 20
不加锁的测试结果:$user['pay'] == 10
下面放ajax图
1、加锁版


2、不加锁版


我的测试方法当然不够准确, 不过已经达到了我的目的
最佳答案
