Question:
What are the rules/logic behind persistent connection management when using PDO?
Environment:
Web Server
- Windows 7 x64
- Dual-core with 16GB RAM
- Apache 2.2.17
- PHP 5.3.5
- Connecting through DSN string with IP address, port, service name, etc...
- No ODBC for DB conn (been trying to create one for 2 hours now, thanks Oracle!)
DB Server
- Oracle 10g on Linux
- Multi-core with 4GB RAM
- Username specifically created for my web app (yes, it's fake)
My understanding/observations:
Non-persistent connections
<?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?
?>
- Script reliably takes about ~.09 seconds to execute with framework overhead, etc...
Persistent connections
<?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
?>
- Script takes ~.12 seconds to execute upon initial visit with framework overhead, etc...
- Sub-sequent executes take ~.04
The issue:
I visit the page and webuser
gets a SID=1
My colleague visits the page and webuser
gets an additional SID=2
<- rinse, repeat, and increment SID for new computers visiting this page
Shouldn't a new visitor be re-using SID=1
?
All answers, suggestions, requests for alternate testing, links to reading material are welcomed.
I have RTFM'ed for a while and Googling has only produced meager Advantages of Persistent vs. Non-persistent
blogs.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…