[wp-trac] [WordPress Trac] #31018: Persistent database connections with mysqli

WordPress Trac noreply at wordpress.org
Thu Sep 3 22:04:13 UTC 2015


#31018: Persistent database connections with mysqli
-------------------------------------+-----------------------
 Reporter:  blobaugh                 |       Owner:
     Type:  enhancement              |      Status:  reopened
 Priority:  normal                   |   Milestone:
Component:  Database                 |     Version:  4.2
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:
-------------------------------------+-----------------------
Changes (by MrGregWaugh):

 * status:  closed => reopened
 * resolution:  wontfix =>


Comment:

 So I'm going to make one more stab at this.  I agree with most here that
 while persistent database connections are generally not needed, there are
 specific cases where they are of great benefit, the example of using
 ClearDB being one.  Seeing "solutions" like the "persistent-database-
 connection-updater" make me really sad.

 The only thing really preventing one from being able to use valid mysqli
 host syntax in wp-config.php for persistent connections (prepending a
 'p:') is the port_or_socket() code in wp-db.php, which according to the
 comments "duplicates how mysql_connect detects a port and/or socket file".
 My proposed solution is to enable that to correctly pass through a 'p:'
 from DB_HOST to mysqli_real_connect.

 For example, you would be able to do:

 {{{define('DB_HOST', 'p:db-host.expensive-connection.com');}}}

 Note: this patch is intended to enable this functionality with absolutely
 minimal changes to the core code.

 [https://core.trac.wordpress.org/attachment/ticket/31018/31018-3.diff
 Patch_31018-3] ([https://core.trac.wordpress.org/raw-
 attachment/ticket/31018/31018-3.diff raw])

 Here is the complete code segment, from wp-db.php line 1431

 {{{
                         // mysqli_real_connect doesn't support the host
 param including a port or socket
                         // like mysql_connect does. This duplicates how
 mysql_connect detects a port and/or socket file.
                         $port = null;
                         $socket = null;
                         $host = $this->dbhost;
                         $pre_host = '';
                         // If DB_HOST begins with a 'p:', allow it to be
 passed to mysqli_real_connect().
                         // mysqli supports persistent connections starting
 with PHP 5.3.0.
                         if (version_compare( phpversion(), '5.3.0', '>=' )
 && 0 === strpos( $host, 'p:' )) {
                                 $host = substr( $host, 2 );
                                 $pre_host = 'p:';
                         }
                         $port_or_socket = strstr( $host, ':' );
                         if ( ! empty( $port_or_socket ) ) {
                                 $host = substr( $host, 0, strpos( $host,
 ':' ) );
                                 $port_or_socket = substr( $port_or_socket,
 1 );
                                 if ( 0 !== strpos( $port_or_socket, '/' )
 ) {
                                         $port = intval( $port_or_socket );
                                         $maybe_socket = strstr(
 $port_or_socket, ':' );
                                         if ( ! empty( $maybe_socket ) ) {
                                                 $socket = substr(
 $maybe_socket, 1 );
                                         }
                                 } else {
                                         $socket = $port_or_socket;
                                 }
                         }
                         $host = $pre_host . $host;

                         if ( WP_DEBUG ) {
                                 mysqli_real_connect( $this->dbh, $host,
 $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
                         } else {
                                 @mysqli_real_connect( $this->dbh, $host,
 $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags );
                         }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31018#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list