最近和朋友打算搞個網站,試水階段不想投入太多資金,同時要有現成的開源代碼可參考,因此PHP是首選了,一個虛擬主機一年才400塊,1G空間還和MySQL共享空間,真便宜啊~~~ php
之前一直用.net開發,習慣了強類型語言的嚴謹,忽然使用動態的PHP,還有些不太適應,言歸正傳吧。 mysql
電商網站最核心的訂單的處理,如何在大併發時候產生連續的訂單編號是一個關鍵。咱們首先想到的天然是自增加字段,咱們如何得到這個自增加字段呢? sql
SQL Server數據庫可使用存儲過程,獲取Scope內插入數據的Identity數值,MySQL如何實現呢?
查閱資料後,我發現PHP已經封裝了MySQL的last_insert_id() API,經測試在獨立鏈接的狀況下,徹底沒有問題。此函數返回的是當前鏈接最後一次插入數據的值(與表無關),那麼若是是持久鏈接會是怎麼樣呢?仍是用代碼來測試一下吧,事實勝於雄辯。 數據庫
一、當即結束的頁面 apache
<?php $pdo = new PDO('mysql:dbname=test;host=localhost;', 'root', ''); $pdo->setAttribute(PDO::ATTR_PERSISTENT, true); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("INSERT INTO testtable (custname) values ('insert1')"); echo 'Last insert id is ' . $pdo->lastInsertId(); ?>
二、休眠10秒的頁面 數組
<?php $pdo = new PDO('mysql:dbname=test;host=localhost;', 'root', ''); $pdo->setAttribute(PDO::ATTR_PERSISTENT, true); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("INSERT INTO testtable (custname) values ('insert2')"); sleep(10); echo 'Last insert id is ' . $pdo->lastInsertId(); ?>執行頁面2後,不停舒心頁面1,ID不斷增長,待頁面2執行完畢後查看,頁面2獲取的ID並不是隨着頁面1增長,也就是說在持久鏈接的狀況下,Last_Insert_ID是有效的!
查看MySQL的鏈接狀況: 併發
mysql> show processlist;
+----+------+----------------+------+---------+------+-------+------------------
+
| Id | User | Host | db | Command | Time | State | Info
|
+----+------+----------------+------+---------+------+-------+------------------
+
| 1 | root | localhost:2206 | NULL | Query | 0 | NULL | show processlist
|
| 3 | root | localhost:2214 | test | Sleep | 2539 | | NULL
|
| 9 | root | localhost:2225 | test | Sleep | 8764 | | NULL
|
| 10 | root | localhost:2227 | test | Sleep | 2537 | | NULL
|
+----+------+----------------+------+---------+------+-------+------------------
+
4 rows in set (0.19 sec) 函數
持久鏈接已經啓用。 測試
反思API的說明,咱們能夠認爲在一個頁面執行完畢(或者說期中數據庫操做部分)前,其所得到的數據鏈接時沒有返回到鏈接池中的,所以PDO::lastInsertId()也是仍然是當前鏈接,而不會返回成其餘鏈接的數據。 網站
但願高手批評指正。
補充,看到一篇轉載的文章,做爲本文補充閱讀:
http://my.oschina.net/fz04003/blog/63327
PHP官網的Note:
若是想使用持久鏈接,必須在傳遞給 PDO 構造函數的驅動選項數組中設置 PDO::ATTR_PERSISTENT 。若是是在對象初始化以後用 PDO::setAttribute() 設置此屬性,則驅動程序將不會使用持久鏈接。
因此以上測試是基於單次鏈接,並不是持久連接,再次消毒!
<?php $DBOptions = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); $pdo = new PDO('mysql:dbname=test;host=localhost', 'root', '', $DBOptions); // $pdo->setAttribute(PDO::ATTR_PERSISTENT, true); // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec("insert into testid (owner) values('2')"); sleep(5); $lastid = $pdo->lastInsertId(); echo 'Inserted ID is ' . $lastid; $pdo->exec("insert into saveid (owner, oid) values ('2', '$lastid')");
使用apache ab 進行休眠與非休眠兩個進程測試:
D:\xampp\apache\bin\ab.exe -n 5000 -c 5 http://localhost/PHPTest/PDOTest/Insert1.php
Server Software: Apache/2.2.11
Server Hostname: localhost
Server Port: 80
Document Path: /PHPTest/PDOTest/Insert2.php
Document Length: 20 bytes
Concurrency Level: 5
Time taken for tests: 20.213 seconds
Complete requests: 20
Failed requests: 0
Write errors: 0
Total transferred: 4840 bytes
HTML transferred: 400 bytes
Requests per second: 0.99 [#/sec] (mean)
Time per request: 5053.260 [ms] (mean)
Time per request: 1010.652 [ms] (mean, across all concurrent requests)
Transfer rate: 0.23 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 1
Processing: 5024 5047 9.8 5048 5067
Waiting: 5023 5046 10.0 5047 5067
Total: 5024 5047 9.8 5049 5067
Percentage of the requests served within a certain time (ms)
50% 5049
66% 5051
75% 5052
80% 5052
90% 5067
95% 5067
98% 5067
99% 5067
100% 5067 (longest request)
MySQL進程
mysql> show processlist;
+----+------+-----------------+------+---------+------+---------------+---------
-------------------------------+
| Id | User | Host | db | Command | Time | State | Info
|
+----+------+-----------------+------+---------+------+---------------+---------
-------------------------------+
| 1 | root | localhost:18353 | test | Sleep | 10 | | NULL
|
| 2 | root | localhost:19964 | NULL | Query | 0 | NULL | show pro
cesslist |
| 3 | root | localhost:19968 | test | Sleep | 459 | | NULL
|
| 4 | root | localhost:20016 | test | Query | 0 | freeing items | insert i
nto testid (owner) values('1') |
| 5 | root | localhost:20020 | test | Sleep | 2 | | NULL
|
| 6 | root | localhost:20023 | test | Query | 0 | freeing items | insert i
nto testid (owner) values('1') |
| 7 | root | localhost:20025 | test | Sleep | 2 | | NULL
|
| 8 | root | localhost:20056 | test | Sleep | 2 | | NULL
|
| 9 | root | localhost:20058 | test | Sleep | 2 | | NULL
|
| 10 | root | localhost:20061 | test | Sleep | 0 | | NULL
|
| 11 | root | localhost:20193 | test | Query | 0 | freeing items | insert i
nto testid (owner) values('1') |
| 12 | root | localhost:20594 | test | Query | 0 | freeing items | insert i
nto testid (owner) values('1') |
| 13 | root | localhost:20614 | test | Sleep | 2 | | NULL
|
| 14 | root | localhost:21587 | test | Query | 0 | freeing items | insert i
nto testid (owner) values('1') |
+----+------+-----------------+------+---------+------+---------------+---------
-------------------------------+
14 rows in set (0.00 sec)
測試結果,從數據庫中查看返回數據正常,沒有重複的ID。持久鏈接可行!
freeing items The thread has executed a command. This state is usually followed by cleaning up.