問(wèn)題描述
使用 PDO 時(shí)持久連接管理背后的規(guī)則/邏輯是什么?
What are the rules/logic behind persistent connection management when using PDO?
網(wǎng)絡(luò)服務(wù)器
- Windows 7 x64
- 雙核,16GB RAM
- Apache 2.2.17
- PHP 5.3.5
- 通過(guò)帶有 IP 地址、端口、服務(wù)名稱等的 DSN 字符串連接...
- 沒(méi)有用于 DB conn 的 ODBC(現(xiàn)在已經(jīng)嘗試創(chuàng)建一個(gè) 2 小時(shí)了,感謝 Oracle!)
數(shù)據(jù)庫(kù)服務(wù)器
- Linux 上的 Oracle 10g
- 具有 4GB RAM 的多核
- 專門為我的網(wǎng)絡(luò)應(yīng)用創(chuàng)建的用戶名(是的,它是假的)
- 用戶:網(wǎng)絡(luò)用戶
非持久連接
<?php // Open a new connection // Session created in Oracle $dbh = new PDO('DSN', 'webuser', 'password'); // webuser is active in v$session with a SID=1 $dbh = NULL; // webuser removed from v$session // Manually calling $dbh = NULL; will remove the session from v$session // OR // Wait for script EOL so a kill-session command is sent to Oracle? ?>
- 腳本可靠地執(zhí)行大約需要大約 0.09 秒,框架開(kāi)銷等...
持久連接
<?php // Open a new connection and make it persistent // Session created in Oracle // Is Apache maintaining some sort of keep-alive with Oracle here? // because I thought php.exe is only alive for the duration of the script $dbh = new PDO('DSN', 'webuser', 'password', array(PDO::ATTR_PERSISTENT => TRUE)); // webuser is active in v$session with a SID=1 $dbh = NULL; // webuser is still active in v$session with a SID=1 $dbh = new PDO('DSN', 'webuser', 'password', array(PDO::ATTR_PERSISTENT => TRUE)); // webuser is still active in v$session with a SID=1 // Manually calling $dbh = NULL; does not kill session // OR // Script EOL does not kill session // ^^ this is good, just as expected ?>
- 腳本在初次訪問(wèn)時(shí)需要約 .12 秒來(lái)執(zhí)行,并帶有框架開(kāi)銷等......
- 后續(xù)執(zhí)行 take ~.04
我訪問(wèn)該頁(yè)面,
webuser
得到一個(gè)SID=1
I visit the page and
webuser
gets aSID=1
我的同事訪問(wèn)了該頁(yè)面,
webuser
獲得了一個(gè)額外的SID=2
<- 為訪問(wèn)此頁(yè)面的新計(jì)算機(jī)沖洗、重復(fù)和增加 SIDMy colleague visits the page and
webuser
gets an additionalSID=2
<- rinse, repeat, and increment SID for new computers visiting this page新訪問(wèn)者不應(yīng)該重復(fù)使用
SID=1
嗎?Shouldn't a new visitor be re-using
SID=1
?歡迎所有答案、建議、替代測(cè)試請(qǐng)求、閱讀材料鏈接.
All answers, suggestions, requests for alternate testing, links to reading material are welcomed.
我已經(jīng)使用 RTFM 一段時(shí)間了,而谷歌搜索只產(chǎn)生了微薄的
持久性與非持久性的優(yōu)勢(shì)
博客.I have RTFM'ed for a while and Googling has only produced meager
Advantages of Persistent vs. Non-persistent
blogs.推薦答案
Apaches 的觀點(diǎn)
Apache 有一個(gè)父進(jìn)程.此進(jìn)程創(chuàng)建子進(jìn)程來(lái)處理傳入 Web 服務(wù)器的任何請(qǐng)求.Web 服務(wù)器啟動(dòng)時(shí)啟動(dòng)的初始子進(jìn)程數(shù)量由 apache 配置中的
StartServers
指令配置.這個(gè)數(shù)字會(huì)隨著訪問(wèn) Web 服務(wù)器的請(qǐng)求數(shù)量的增加而增加,直到達(dá)到ServerLimit
.Apaches point of view
Apache has one parent process. This process creates child processes that will handle any requests coming to the web server. The initial amount of child processes being started when the web server starts is configured by the
StartServers
directive in the apache configuration. The number goes up as needed with a raising amount of requests hitting the web server untilServerLimit
is reached.如果 PHP(作為 mod_php 運(yùn)行,作為 CGI 在腳本執(zhí)行結(jié)束時(shí)所有資源都被釋放)現(xiàn)在被告知為請(qǐng)求建立與數(shù)據(jù)庫(kù)的持久連接,即使在腳本完成后,該連接也會(huì)保持.現(xiàn)在保持的連接是處理請(qǐng)求的 apache 子進(jìn)程和數(shù)據(jù)庫(kù)服務(wù)器之間的連接,并且可以被這個(gè)確切的子進(jìn)程正在處理的任何請(qǐng)求重用.
If PHP (ran as mod_php, as CGI all resources are freed at the end of script execution) is now being told to establish a persistent connection with a database for a request, this connection is hold even after the script finishes. The connection being now hold is a connection between the apache child process which the request was handled by and the database server and can be re-used by any request that is being handled by this exact child process.
如果由于某種原因(不要問(wèn)我確切的原因),子進(jìn)程被占用的時(shí)間比實(shí)際請(qǐng)求的時(shí)間長(zhǎng)并且另一個(gè)請(qǐng)求進(jìn)來(lái),父 apache 進(jìn)程將此請(qǐng)求重定向到一個(gè)(新)子進(jìn)程,這可能到目前為止還沒(méi)有建立到數(shù)據(jù)庫(kù)的連接.如果在腳本執(zhí)行期間必須這樣做,它會(huì)像您觀察到的那樣引發(fā) SID.現(xiàn)在有兩個(gè)連接被apache的兩個(gè)不同的子進(jìn)程持有.
If, for some reason (do not ask me exactly why), the child process is being occupied longer than the actual request and another request comes in, the parent apache process redirects this request to a (new) child process which may has not established a connection to the database up to this time. If it has to during the execution of the script, it raises the SID as you have observed. Now there are two connections be hold by two different child processes of apache.
重要的是要知道,這也會(huì)引起很多麻煩.如果在腳本執(zhí)行過(guò)程中出現(xiàn)死循環(huán)或中止事務(wù)或其他一些甚至不可預(yù)測(cè)的錯(cuò)誤,連接將被阻塞且無(wú)法重用.也可能發(fā)生數(shù)據(jù)庫(kù)的所有可用連接都被使用,但 apache 服務(wù)器的另一個(gè)子進(jìn)程試圖訪問(wèn)數(shù)據(jù)庫(kù).該進(jìn)程暫時(shí)被阻止,直到數(shù)據(jù)庫(kù)或 apache 釋放連接(超時(shí)或自愿終止).此頁(yè)面上有關(guān)此主題的任何進(jìn)一步信息:http://www.php.net/manual/en/features.persistent-connections.php
It is important to know, that this can also cause a lot of trouble. If there is an endless loop or an aborted transaction or some other may be even unpredictable error during the script execution, the connection is blocked and can not be re-used. Also it could happen that all of the available connections of the database are used, but there is another child process of the apache server trying to access the database. This process is blocked for the time being until a connection is freed by the database or apache (timeout or voluntarily by termination). Any further information about this topic on this page: http://www.php.net/manual/en/features.persistent-connections.php
我希望我能正確總結(jié)我們?cè)谠u(píng)論對(duì)話中討論的所有內(nèi)容,并且沒(méi)有忘記任何事情.如果是這樣,請(qǐng)給我一個(gè)提示,我會(huì)添加它.:)
I hope I got all that we have discussed in our comment conversation summarized correctly and did not forget anything. If so, please, leave me a hint and I will add it. :)
我剛剛讀完了這篇評(píng)論.它描述了我在上面總結(jié)的過(guò)程,并提供了有關(guān)如何優(yōu)化 apache 服務(wù)器以更好地與持久連接一起工作的有用信息.不過(guò),它可以在有或沒(méi)有 oracle 數(shù)據(jù)庫(kù)后端的情況下使用.你應(yīng)該看看:http://www.oracle.com/technetwork/articles/coggeshall-persist-084844.html
I just finished reading the article @MonkeyZeus mentioned in this comment. It describes the process I summarized above and provides useful information on how to optimize your apache server to work better together with persistent connections. It can be used with or without oracle database backends, though. You should give a look: http://www.oracle.com/technetwork/articles/coggeshall-persist-084844.html
這篇關(guān)于完全理解 PDO ATTR_PERSISTENT的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!