Yii+MYSQL lock table method to prevent duplicate data under concurrency
Mar 30, 2017 pm 03:17 PMThe example of this article describes the method of Yii+MYSQL lock table to prevent duplicate data in concurrent situations. Share it with everyone for your reference, as follows:
lock table read lock
If a thread obtains a read lock on a table, then the thread and all other threads can only read locks from the table Read data, no write operations can be performed.
lock tables user read;//讀鎖定表 unlock tables;//解鎖 lock tables user read local;//本地讀鎖定表,其他線程的insert未被阻塞,update操作被阻塞
lock table write lock
If a thread obtains a write lock on a table, then only the thread that owns the lock can read from and write to the table. Other threads are blocked.
lock tables user write;//寫鎖定表 unlock tables;//解鎖
Usage examples in Yii
/** * 當(dāng)日單項(xiàng)內(nèi)容狀態(tài) */ public function getPointAready($marke,$dayTime){ $model = SysRun::model()->findByAttributes(array('syr_marking'=>$marke,'syr_daytime'=>$dayTime)); if(empty($model)){ //表寫鎖定 Yii::app()->db->createCommand()->setText("lock tables {{sys_run}} WRITE")->execute(); $model = new SysRun(); $model->syr_marking = $marke; $model->syr_daytime = $dayTime; $model->syr_val = 0; $model->syr_subval = 0; $model->save(); //表解鎖 Yii::app()->db->createCommand()->setText("unlock tables")->execute(); } return $model; }
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.
For more related articles on how Yii+MYSQL locks tables to prevent duplicate data under concurrency, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)