Say I have this code:
<?php
$pdo = new PDO(
'mysql:host=127.0.0.1;dbname=test_sql',
'root',
'',
array(
PDO::ATTR_PERSISTENT => true));
header('Content-Type: text/plain');
for ($i = 30; $i > 0; $i--) {
echo 'SQL says ' . $pdo->query('SELECT CONCAT(CONNECTION_ID(), "/", NOW()) AS x')->fetchAll(PDO::FETCH_ASSOC)[0]['x'] . ' END|';
sleep(1);
}
It's run in Apache, and so if I open a browser to this page and run SHOW PROCESSLIST
in mysql, then I see 1 connection. Once the page has done loading (after 30 seconds), I will still see that connection in MySQL (because it's persistent, ok).
But now, if I open 2 tabs on this same page (no matter whether it's same browser, we don't care about that here since it has no impact), then I see 2 connections in the SHOW PROCESSLIST
.
So I suppose PHP keeps the persistent connection open, but won't share it between "PHP instances". Is there a way to do so? To have 1 single connection to the mysql server running for the 2 PHP instances (no matter whether this connection is persistent or not, but I doubt non-persistent connection would work)?