1, why should I lock it?
When there is a small amount of access to a feature, you can add log directly and then determine whether the operation was done based on the record. But once I was working on a batch of data--Scheduling tasks
In the automatic execution of that script, customer service in the manual execution of the script, the result create_time the same number of seconds, this time the query results and if statement is not limited, need to use
Lock.
When multiple scripts concurrently access the server side at the same time, the access script will be incorrect, the problem needs to be locked (locking mechanism) to resolve.
2,mysql Lock
(1), read lock, lock process all clients read this table only.
(2), write lock, only the locked table customer service can operate, the other can only be released to this lock release.
Locking: LOCK Table Table 1 read| WRITE, table 2 read| WRITE ...
Unlock: UNLOCK TABLES (TABLES unlock multiple tables)
3, the file lock code is as follows:
Build a file
$f = fopen ('/tmp/'. $task [' id ']. ". txt", "w+");
Locked
Flock ($f, LOCK_EX);
Here is the business code
The middle code can only be accessed by one person at a time, and the principle is to build a file allowing only one person to enter and exit
Release lock
Flock ($f, LOCK_EX);
Fclose ($f);
deleting files
Unlink ('/tmp/'. $task [' id ']. ". TXT ");
PHP Lockdown Overview