php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54181 MySQLi doesn't always reuse persistent link inside same script
Submitted: 2011-03-07 15:10 UTC Modified: 2011-03-08 09:58 UTC
From: carsten_sttgt at gmx dot de Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: carsten_sttgt at gmx dot de
New email:
PHP Version: OS:

 

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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-07 17:28 UTC] scottmac@php.net
-Status: Open +Status: Bogus
 [2011-03-07 17:28 UTC] scottmac@php.net
This doesn't have anything to do with persistent connections, with mysql_connect() 
if you open a connection with the same parameters it won't return a new link 
unless you set the 4th parameter to true.

This is working as expected, persistent connections are only re-used if they're no 
longer in use by another script.
 [2011-03-07 19:53 UTC] carsten_sttgt at gmx dot de
> 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.
 [2011-03-08 09:34 UTC] scottmac@php.net
The problem with using the same connection twice is that with unbuffered results 
you can't run more than query at once. MySQLi only puts a connection up for availability until after its done with it.

MySQL should behave the same but is probably affected by the new_link parameter 
which returned the same connection if all the parameters matched. It wasn't 
persistance just an optimization that caused more confusion than anything else.
 [2011-03-08 09:58 UTC] carsten_sttgt at gmx dot de
> 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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 15 00:01:27 2024 UTC