PHP????????????????
???????????? ???????[ 2015/8/21 13:52:51 ] ?????????????
??????????????????????????UPDATE??????????????£?????????????????????????????????а?????????????У?
????1 mysql> select * from counter;
????2 +----+------+
????3 | id | num |
????4 +----+------+
????5 | 1 | 21495|
????6 +----+------+
????7 1 row in set (0.00 sec)
??????????????????????????????????????????????????????????????????
1 <?php
2 function dummy_business() {
3 $conn = mysqli_connect('127.0.0.1'?? 'public'?? 'public') or die(mysqli_error());
4 mysqli_select_db($conn?? 'test');
5 for ($i = 0; $i < 10000; $i++) {
6 mysqli_query($conn?? 'BEGIN');
7 $rs = mysqli_query($conn?? 'SELECT num FROM counter WHERE id = 1');
8 mysqli_free_result($rs);
9 $row = mysqli_fetch_array($rs);
10 $num = $row[0];
11 mysqli_query($conn?? 'UPDATE counter SET num = '.$num.' + 1 WHERE id = 1');
12 if(mysqli_errno($conn)) {
13 mysqli_query($conn?? 'ROLLBACK');
14 } else {
15 mysqli_query($conn?? 'COMMIT');
16 }
17 }
18 mysqli_close($conn);
19 }
20
21 for ($i = 0; $i < 10; $i++) {
22 $pid = pcntl_fork();
23
24 if($pid == -1) {
25 die('can not fork.');
26 } elseif (!$pid) {
27 dummy_business();
28 echo 'quit'.$i.PHP_EOL;
29 break;
30 }
31 }
32 ?>
????????????????
????1 mysql> select * from counter;
????2 +----+------+
????3 | id | num |
????4 +----+------+
????5 | 1 | 16328|
????6 +----+------+
????7 1 row in set (0.00 sec)
?????????????????????????????????????????????ACID???????????????????????????в??????????????£????????????????????????????
????????????????????????????????????????£????????????????μ?????
?????????????????????????????δ?????????
?????????????????????????ζ??????????????ζ?????????????
?????????????????????ζ???????Χ?????????ζ??????????????
???????????????????????????????
?????????? ??? ????????? ???
????Read uncommitted √ √ √
????Read committed × √ √
????Repeatable read × × √
????Serializable × × ×
????????????????????????????????????Read committed??????MySQL????????????????????Repeatable read?????????????£?????????л???Serializable????????????????????????????????????????????л???????????????????????????????????????????????????????????????????
???????????У?????????Pessimistic Locking???????????Optimistic Locking????????????????
???????????????????????????????????????????????????????????????????????????????????????????????????????????SELECT…FOR UPDATE?????
?????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????????????????
1 <?php
2 function dummy_business() {
3 $conn = mysqli_connect('127.0.0.1'?? 'public'?? 'public') or die(mysqli_error());
4 mysqli_select_db($conn?? 'test');
5 for ($i = 0; $i < 10000; $i++) {
6 mysqli_query($conn?? 'BEGIN');
7 $rs = mysqli_query($conn?? 'SELECT num FROM counter WHERE id = 1 FOR UPDATE');
8 if($rs == false || mysqli_errno($conn)) {
9 // ???????
10 mysqli_query($conn?? 'ROLLBACK');
11 // ??????б??β???
12 $i--;
13 continue;
14 }
15 mysqli_free_result($rs);
16 $row = mysqli_fetch_array($rs);
17 $num = $row[0];
18 mysqli_query($conn?? 'UPDATE counter SET num = '.$num.' + 1 WHERE id = 1');
19 if(mysqli_errno($conn)) {
20 mysqli_query($conn?? 'ROLLBACK');
21 } else {
22 mysqli_query($conn?? 'COMMIT');
23 }
24 }
25 mysqli_close($conn);
26 }
27
28 for ($i = 0; $i < 10; $i++) {
29 $pid = pcntl_fork();
30
31 if($pid == -1) {
32 die('can not fork.');
33 } elseif (!$pid) {
34 dummy_business();
35 echo 'quit'.$i.PHP_EOL;
36 break;
37 }
38 }
39 ?>
??????????????????????????????????????
????1 mysql> select * from counter;
????2 +----+--------+
????3 | id | num |
????4 +----+--------+
????5 | 1 | 100000 |
????6 +----+--------+
????7 1 row in set (0.00 sec)
????????????????????????????????????????????????????????????MySQL Inodb?????????????????????????????????????????????Χ??????????????????????????
??????????????????????????????????????????????????counter???????????Σ?
????1 mysql> select * from counter;
????2 +----+------+---------+
????3 | id | num | version |
????4 +----+------+---------+
????5 | 1 | 1000 | 1000 |
????6 +----+------+---------+
????7 1 row in set (0.01 sec)
????????????£?
1 <?php
2 function dummy_business() {
3 $conn = mysqli_connect('127.0.0.1'?? 'public'?? 'public') or die(mysqli_error());
4 mysqli_select_db($conn?? 'test');
5 for ($i = 0; $i < 10000; $i++) {
6 mysqli_query($conn?? 'BEGIN');
7 $rs = mysqli_query($conn?? 'SELECT num?? version FROM counter WHERE id = 1');
8 mysqli_free_result($rs);
9 $row = mysqli_fetch_array($rs);
10 $num = $row[0];
11 $version = $row[1];
12 mysqli_query($conn?? 'UPDATE counter SET num = '.$num.' + 1?? version = version + 1 WHERE id = 1 AND version = '.$version);
13 $affectRow = mysqli_affected_rows($conn);
14 if($affectRow == 0 || mysqli_errno($conn)) {
15 // ?????????????
16 mysqli_query($conn?? 'ROLLBACK');
17 $i--;
18 continue;
19 } else {
20 mysqli_query($conn?? 'COMMIT');
21 }
22 }
23 mysqli_close($conn);
24 }
25
26 for ($i = 0; $i < 10; $i++) {
27 $pid = pcntl_fork();
28
29 if($pid == -1) {
30 die('can not fork.');
31 } elseif (!$pid) {
32 dummy_business();
33 echo 'quit'.$i.PHP_EOL;
34 break;
35 }
36 }
37 ?>
??????Σ?????????????????????
????1 mysql> select * from counter;
????2 +----+--------+---------+
????3 | id | num | version |
????4 +----+--------+---------+
????5 | 1 | 100000 | 100000 |
????6 +----+--------+---------+
????7 1 row in set (0.01 sec)
?????????????????е???????????UPDATE??????????????????????ú??
?????????Doctrine ORM????????У?Doctrine?????????????????????????????????÷????ο????
????http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/transactions-and-concurrency.html#locking-support
????Hibernate?????????????????????????????????????
??????????????д??????????????????????????????????????????????Ч?????????????????????????NoSQL???????????е??????????Ч?????????????
?????ο?????
??????????????????
????MySQL??????????????????
?????????????????????????
???????????????
????????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
?????????????????????????Щ????????????????????TC???????????????Щ???????????????????????????????????????????????(java .net ?????)???mysql???????????????????ж????д???Python???????????????(DB2)??????BufferPool????????????????????????????????6??????????????????滮???????????????-????????SQL Server???????????????????λ?????PHP??SQL????????????????????Pythonд???NoSQL????????? SQL ?е????????????? SQL ?е?????????Java???????:?????MySQL???????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????