Description
Hello,
Maybe I misunderstood something in the code but it seems the shim is managing a connection pool for non-persistent connections. For persistent connections, it relays on the mysqli extension.
Is this to reproduce behavior from the deprecated mysql extension? If not, why is the shim caching non persistent connections? Would it be unadvisable to switch off this behavior?
From the mysqli doc with respect to persistent connections:
The mysqli extension supports persistent database connections, which are a special kind of pooled connections. By default, every database connection opened by a script is either explicitly closed by the user during runtime or released automatically at the end of the script. A persistent connection is not. Instead it is put into a pool for later reuse, if a connection to the same server using the same username, password, socket, port and default database is opened. Reuse saves connection overhead.
In the shim , for non-persistent connections, mysql_connect() is looking up in a pool if the connection already exists using a hash of the connection characteristics. If it exists, it increases a reference counter and returns the existing connection (lines 66-76), thus making a connection pool:
Lines 66 to 76 in 98f8350
Further down in mysql_connect(), the hash is always associated with the connection:
Lines 90 to 91 in 98f8350
Lines 118 to 119 in 98f8350
In mysql_close(), the references to the connection are decreased by one and if the connection has zero references, then it is closed. Again, this is managing a connection pool.
Lines 153 to 161 in 98f8350
Note that there is a potential error in that code as the there is no check to see if the hash exists. I think it should be factorized as:
if (isset(MySQL::$connections[$link->hash])) {
MySQL::$connections[$link->hash]['refcount'] -= 1;
if (MySQL::$connections[$link->hash]['refcount'] === 0) {
$return = mysqli_close($link);
unset(MySQL::$connections[$link->hash]);
} else {
$return = true;
}
}
Thanks for your consideration.