0

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)?

Xenos
  • 3,351
  • 2
  • 27
  • 50

1 Answers1

0

PHP doesn't have a real connection pool. Each "PHP Instance" keeps and reuses its own persistent connection.

There is no way to have multiple PHP processes/threads share their connections.

Maybe there is a 3-rd party proxy which does this, but that doesn't really count :)

Since it's usually not desirable to bottleneck everything into a limited amount of MySQL threads, sharing a small amount of connections is also not beneficial.

Vatev
  • 7,493
  • 1
  • 32
  • 39