最近和朋友打算搞個(gè)網(wǎng)站,試水階段不想投入太多資金,同時(shí)要有現(xiàn)成的開源代碼可參考,所以PHP是選擇了,一個(gè)虛擬主機(jī)一年才400塊,1G空間還和MySQL共享空間,真便宜啊~~~
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、偃師網(wǎng)站維護(hù)、網(wǎng)站推廣。以前一直用.net開發(fā),習(xí)慣了強(qiáng)類型語(yǔ)言的嚴(yán)謹(jǐn),突然使用動(dòng)態(tài)的PHP,還有些不太適應(yīng),言歸正傳吧。
電商網(wǎng)站最核心的訂單的處理,如何在大并發(fā)時(shí)候產(chǎn)生連續(xù)的訂單編號(hào)是一個(gè)關(guān)鍵。我們首先想到的自然是自增長(zhǎng)字段,我們?nèi)绾潍@得這個(gè)自增長(zhǎng)字段呢?
SQL Server數(shù)據(jù)庫(kù)可以使用存儲(chǔ)過(guò)程,獲取Scope內(nèi)插入數(shù)據(jù)的Identity數(shù)值,MySQL如何實(shí)現(xiàn)呢?
查閱資料后,我發(fā)現(xiàn)PHP已經(jīng)封裝了MySQL的last_insert_id() API,經(jīng)測(cè)試在獨(dú)立連接的情況下,完全沒有問(wèn)題。此函數(shù)返回的是當(dāng)前連接最后一次插入數(shù)據(jù)的值(與表無(wú)關(guān)),那么如果是持久連接會(huì)是怎么樣呢?還是用代碼來(lái)測(cè)試一下吧,事實(shí)勝于雄辯。
1、立即結(jié)束的頁(yè)面
<?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(); ?>
2、休眠10秒的頁(yè)面
<?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(); ?> 執(zhí)行頁(yè)面2后,不停舒心頁(yè)面1,ID不斷增加,待頁(yè)面2執(zhí)行完畢后查看,頁(yè)面2獲取的ID并非隨著頁(yè)面1增加,也就是說(shuō)在持久連接的情況下,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)
持久連接已經(jīng)啟用。
反思API的說(shuō)明,我們可以認(rèn)為在一個(gè)頁(yè)面執(zhí)行完畢(或者說(shuō)期中數(shù)據(jù)庫(kù)操作部分)前,其所獲得的數(shù)據(jù)連接時(shí)沒有返回到連接池中的,因此PDO::lastInsertId()也是仍然是當(dāng)前連接,而不會(huì)返回成其他連接的數(shù)據(jù)。
希望高手批評(píng)指正。
補(bǔ)充,看到一篇轉(zhuǎn)載的文章,作為本文補(bǔ)充閱讀:
http://my.oschina.net/fz04003/blog/63327
PHP官網(wǎng)的Note:
如果想使用持久連接,必須在傳遞給 PDO 構(gòu)造函數(shù)的驅(qū)動(dòng)選項(xiàng)數(shù)組中設(shè)置PDO::ATTR_PERSISTENT。如果是在對(duì)象初始化之后用PDO::setAttribute()設(shè)置此屬性,則驅(qū)動(dòng)程序?qū)⒉粫?huì)使用持久連接。
所以以上測(cè)試是基于單次連接,并非持久鏈接,再次消毒!<?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 進(jìn)行休眠與非休眠兩個(gè)進(jìn)程測(cè)試:
D:xamppapachebinab.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進(jìn)程
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)
測(cè)試結(jié)果,從數(shù)據(jù)庫(kù)中查看返回?cái)?shù)據(jù)正常,沒有重復(fù)的ID。持久連接可行!
freeing items
The thread has executed a command. This state is usually followed by cleaning up.
新聞名稱:PHP獲取自增長(zhǎng)字段數(shù)值(MySQL的last_insert_id())
文章來(lái)源:http://www.rwnh.cn/article12/cgsidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、商城網(wǎng)站、虛擬主機(jī)、面包屑導(dǎo)航、營(yíng)銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)