Body Content
Simulation preparation-How to simulate high concurrent access to a script: Apache installation file Bin/ab.exe can simulate concurrency
-C simulates how many concurrent-N requests have been requested for a total of/HTTP scripts
C:\phpstudy\apache\bin>ab.exe-c 10-n http://localhost/try.php
The lock in MySQL:
Grammar:
LOCK Table table Name 1 read| WRITE, table name 2 read| WRITE ... "Lock-up table" ... "..".
UNLOCK TABLES "Release table"
READ: Reading Lock | Shared Lock: All clients can only read this table and cannot write this table
Write: Lock | Lock: All currently locked clients can manipulate the table, and other clients will only block
Note: Only locked tables can be manipulated during the lock table, and if you want to manipulate other tables, you must lock all the tables you want to manipulate!
File locks in PHP (locks are files, not tables)
How does a file lock file relate to a table? : There is no relationship, similar to the token, who gets the operation. So the table is not locked at all.
Test, there is a file on the line, what name does not matter
Summarize:
The project should only use the file lock in PHP, as far as possible to avoid the lock table, because if the table is locked, then the entire site and the table related functions are slowed down (for example: Many users of the front desk has been placed orders, product table MySQL lock table, other products related to the operation of the table has been in a blocking state " Can't read the product list "because a feature slows down the entire site."
For example, in an out-of-order takeout project, 12-2 noon, 6 o'clock in the evening are orders high concurrency, in this case, the MySQL lock is obviously not considered, the user experience is too poor. In fact, according to the actual demand, take-out can not be designed inventory, of course, except the second kill the active module or PHP file lock.
Application Scenarios:
1. When the high concurrent orders, reduce inventory to be locked
2. High concurrent grab orders, to use when robbing tickets
MySQL Lock sample code:
<?PHP/*analog kill activity--100 pieces of CREATE TABLE ta (id int comment ' simulation of the number of 100 items of active goods '); INSERT into Ta VALUES (100); Impersonation: Access this script in 10 concurrent volumes! Using Apache's own Ab.exe software*/ //Turn off Error reporting error_reporting(0); $dbhost= ' localhost:3306 ';//MySQL server host address $dbuser= ' root ';//MySQL user name $dbpass= ' root ';//mysql user name password $conn=Mysqli_connect($dbhost,$dbuser,$dbpass); if(!$conn ) { die(' Connection failed: '.Mysqli_error($conn)); } //set the encoding to prevent Chinese garbled characters Mysqli_query($conn, "Set names UTF8"); mysqli_select_db($conn, ' temp ' ); #MySQL Lock Mysqli_query($conn, ' LOCK TABLE a WRITE ');//only one client can lock the table, and the other clients block the $rs=Mysqli_query($conn, ' SELECT ID from a '); $id= Mysqli_result ($rs, 0, 0); if($id> 0) { --$id; Mysqli_query($conn, ' UPDATE a SET id= '.$id); } #MySQL unlock Mysqli_query($conn, ' UNLOCK TABLES '); //the ID value after the query is unlocked//$res = Mysqli_query ($conn, ' SELECT ID from Ta '); while ($row = Mysqli_fetch_assoc ($res))//{//$id = $row [' id ']; }//echo $id;
PHP file Lock Sample code:
<?PHP/*analog kill activity--100 pieces of CREATE TABLE ta (id int comment ' simulation of the number of 100 items of active goods '); INSERT into Ta VALUES (100); Impersonation: Access this script in 10 concurrent volumes! Using Apache's own Ab.exe software*/ //Turn off Error reporting error_reporting(0); $dbhost= ' localhost:3306 ';//MySQL server host address $dbuser= ' root ';//MySQL user name $dbpass= ' root ';//mysql user name password $conn=Mysqli_connect($dbhost,$dbuser,$dbpass); if(!$conn ) { die(' Connection failed: '.Mysqli_error($conn)); } //set the encoding to prevent Chinese garbled characters Mysqli_query($conn, "Set names UTF8"); mysqli_select_db($conn, ' temp ' ); #file locks in PHP $fp=fopen('./a.lock ', ' R ');//php file Locks and tables Okay, just a file can Flock($fp, LOCK_EX);//Exclusive Lock $retval=Mysqli_query($conn, ' SELECT ID from Ta '); while($row=Mysqli_fetch_assoc($retval)) { $id=$row[' ID ']; } if($id> 0) { --$id; Mysqli_query($conn, ' UPDATE ta SET id= '.$id); } #php file lock, Release lock Flock($fp,Lock_un); fclose($fp); //$res = mysqli_query ($conn, ' SELECT ID from Ta '); while ($row = Mysqli_fetch_assoc ($res))//{//$id = $row [' id ']; }//echo $id;
article from : Blog
Links: http://www.cnblogs.com/foxy/p/9204892.html
MySQL lock mechanism &&php lock mechanism, in which scenarios are applied?