Lock table read lock
If a thread acquires a read lock on a table, the thread and all other threads can only read data from the table and cannot perform any write operations.
Lock tables user read; // read the locked table
Unlock tables; // unlock
Lock tables user read local; // The local read locks the table. The insert statements of other threads are not blocked, and the update operation is blocked.
Lock table write lock
If a thread obtains a write lock on a table, only the thread that owns the lock can read and write tables from the table. Other threads are blocked.
Lock tables user write; // write to lock the table
Unlock tables; // unlock
Usage instance in Yii
/**
* Single content status on the current day
*/
Public function getPointAready ($ marke, $ dayTime ){
$ Model = SysRun: model ()-> findByAttributes (array ('syr _ marking' => $ marke, 'syr _ daytime' => $ daytime ));
If (empty ($ model )){
// Table write lock
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 ();
// Unlock a table
Yii: app ()-> db-> createCommand ()-> setText ("unlock tables")-> execute ();
}
Return $ model;
}