|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-03-07 15:10 UTC] carsten_sttgt at gmx dot de
Description:
------------
If I open an existing persistent link a second (or 3rd, ...) time inside the same script, MySQLi is only reusing the existent link if I have explicitly closed the link before. At the moment it's always creating a new link.
With MySQL it's working as expected.
Test script:
---------------
<?php
$con1 = mysqli_connect('p:localhost', 'foo', '');
var_dump($con1->thread_id);
$con2 = mysqli_connect('p:localhost', 'foo', '');
var_dump($con2->thread_id);
mysqli_close($con1);
mysqli_close($con2);
echo "---\n";
$con3 = mysqli_connect('p:localhost', 'bar', '');
var_dump($con3->thread_id);
mysqli_close($con3);
$con4 = mysqli_connect('p:localhost', 'bar', '');
var_dump($con4->thread_id);
mysqli_close($con4);
echo "===\n";
$con5 = mysql_pconnect('localhost', 'foo', '');
var_dump(mysql_thread_id($con5));
$con6 = mysql_pconnect('localhost', 'foo', '');
var_dump(mysql_thread_id($con6));
mysql_close($con5);
mysql_close($con6);
echo "---\n";
$con7 = mysql_pconnect('localhost', 'bar', '');
var_dump(mysql_thread_id($con7));
mysql_close($con7);
$con8 = mysql_pconnect('localhost', 'bar', '');
var_dump(mysql_thread_id($con8));
mysql_close($con8);
?>
Expected result:
----------------
int(1)
int(1)
---
int(2)
int(2)
===
int(3)
int(3)
---
int(4)
int(4)
Actual result:
--------------
int(1)
int(2)
---
int(3)
int(3)
===
int(4)
int(4)
---
int(5)
int(5)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 15:00:01 2025 UTC |
> This is working as expected, persistent connections are only > re-used if they're no longer in use by another script. OK, with "another Script" you also mean the same script. Just to recapitulate: | <?php | $con1 = mysqli_connect('p:localhost', 'foo', ''); | $con2 = mysqli_connect('p:localhost', 'foo', ''); | ?> $con1 is already in use, so $con2 will results in a second connection. If this is the expected behavior, why is this script: | <?php | $con1 = new PDO( | 'mysql:host=localhost', | 'foo', '', | array(PDO::ATTR_PERSISTENT => true) | ); | $con2 = new PDO( | 'mysql:host=localhost', | 'foo', '', | array(PDO::ATTR_PERSISTENT => true) | ); | ?> or | <?php | $con1 = mysql_pconnect('localhost', 'foo', ''); | $con2 = mysql_pconnect('localhost', 'foo', ''); | ?> resulting in 1 connection? $con1 is already in use, but $con2 is using the same connection as $1.> MySQL should behave the same but is probably affected > by the new_link parameter Ok, this one I can understand (I think...) | $con1 = mysqli_connect('localhost', 'foo', ''); | $con2 = mysqli_connect('localhost', 'foo', ''); is resulting in 2 connections, so: | $con1 = mysqli_connect('p:localhost', 'foo', ''); | $con2 = mysqli_connect('p:localhost', 'foo', ''); is also resulting in 2 connections. | $con1 = mysql_connect('localhost', 'foo', ''); | $con2 = mysql_connect('localhost', 'foo', ''); is resulting in 1 connections, so: | $con1 = mysql_pconnect('localhost', 'foo', ''); | $con2 = mysql_pconnect('localhost', 'foo', ''); is also resulting in 1 connections. (no new_link) But what's with PDO_MySQL? | $con1 = new PDO('mysql:host=localhost', 'foo', ''); | $con2 = new PDO('mysql:host=localhost', 'foo', ''); is resulting in 2 connections, but: | $con1 = new PDO('mysql:host=localhost', 'foo', '', | array(PDO::ATTR_PERSISTENT => true)); | $con2 = new PDO('mysql:host=localhost', 'foo', '', | array(PDO::ATTR_PERSISTENT => true)); is resulting in 1 connections. Thus this "bug" should be assigned to PDO/PDO_MySQL? (Maybe as change request). Or as "Doc" for MySQLi/PDO how this is working in detail. BTW: I think there should be consequent behavior how thinks are working. Or a better description how a extension is working. For mysql_pconnect there is an exact description: | First, when connecting, the function would first try to find a (persistent) | link that's already open with the same host, username and password. If one | is found, an identifier for it will be returned instead of opening a new | connection.